From ba36101a2606bbcf5e5c5fbc1c197dcddc9004f4 Mon Sep 17 00:00:00 2001 From: Null Date: Sat, 10 Oct 2015 14:50:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=94=B9=EF=BC=8C=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E9=83=A8=E5=88=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fps+=200 --- source/Chunk.cpp | 7 +- source/Definitions.cpp | 1 - source/Definitions.h | 5 +- source/Frustum.cpp | 2 +- source/Menus.cpp | 8 +- source/NEWorld.cpp | 33 ++------ source/TextRenderer.cpp | 2 +- source/Textures.cpp | 161 +++++++++++++++++++--------------------- source/Textures.h | 35 +++++---- source/World.cpp | 33 +++----- source/World.h | 4 +- source/WorldGen.cpp | 2 +- source/stdinclude.h | 1 + 13 files changed, 130 insertions(+), 164 deletions(-) diff --git a/source/Chunk.cpp b/source/Chunk.cpp index a3f0f0b..0f13c03 100644 --- a/source/Chunk.cpp +++ b/source/Chunk.cpp @@ -154,7 +154,6 @@ namespace world{ rebuiltChunks++; updatedChunks++; - cindex = getChunkIndex(cx, cy, cz); if (renderBuilt == false){ renderBuilt = true; @@ -167,7 +166,7 @@ namespace world{ for (z = 0; z < 16; z++) { if (pblocks[x*256 + y*16 + z] == blocks::AIR) continue; if (!BlockInfo(pblocks[x*256 + y*16 + z]).isTranslucent()) - renderblock(x, y, z, cindex); + renderblock(x, y, z, this); } } } @@ -179,7 +178,7 @@ namespace world{ for (z = 0; z < 16; z++){ if (pblocks[x*256 + y*16 + z] == blocks::AIR) continue; if (BlockInfo(pblocks[x*256 + y*16 + z]).isTranslucent() && BlockInfo(pblocks[x*256 + y*16 + z]).isSolid()) - renderblock(x, y, z, cindex); + renderblock(x, y, z, this); } } } @@ -191,7 +190,7 @@ namespace world{ for (z = 0; z < 16; z++){ if (pblocks[x*256 + y*16 + z] == blocks::AIR) continue; if (!BlockInfo(pblocks[x*256 + y*16 + z]).isSolid()) - renderblock(x, y, z, cindex); + renderblock(x, y, z, this); } } } diff --git a/source/Definitions.cpp b/source/Definitions.cpp index 308387b..cf878e3 100644 --- a/source/Definitions.cpp +++ b/source/Definitions.cpp @@ -32,7 +32,6 @@ float playerHeightExt; int windowwidth; //窗口宽度 int windowheight; //窗口宽度 bool gamebegin, bagOpened; -int k; TextureID BlockTexture[20]; TextureID BlockTextures; diff --git a/source/Definitions.h b/source/Definitions.h index 684c148..eb654ea 100644 --- a/source/Definitions.h +++ b/source/Definitions.h @@ -1,5 +1,7 @@ #pragma once -#pragma comment(linker, "/SUBSYSTEM:\"WINDOWS,5.01\" /ENTRY:\"mainCRTStartup\"") +#ifndef _DEBUG +#pragma comment(linker, "/SUBSYSTEM:\"WINDOWS\" /ENTRY:\"mainCRTStartup\"") +#endif #include "stdinclude.h" /* @@ -74,7 +76,6 @@ extern float playerHeightExt; extern int windowwidth; extern int windowheight; extern bool gamebegin, bagOpened; -extern int k; extern TextureID BlockTexture[20]; extern TextureID BlockTextures; diff --git a/source/Frustum.cpp b/source/Frustum.cpp index f5105af..7091983 100644 --- a/source/Frustum.cpp +++ b/source/Frustum.cpp @@ -86,7 +86,7 @@ namespace Frustum { bool AABBInFrustum(const Hitbox::AABB& aabb) { for (int i = 0; i < 24; i += 4){ if (frus[i + 0] * aabb.xmin + frus[i + 1] * aabb.ymin + frus[i + 2] * aabb.zmin + frus[i + 3] <= 0.0 && - frus[i + 0] * aabb.xmax + frus[i + 1] * aabb.ymin + frus[i + 2] * aabb.zmin + frus[i + 3] <= 0.0&& + frus[i + 0] * aabb.xmax + frus[i + 1] * aabb.ymin + frus[i + 2] * aabb.zmin + frus[i + 3] <= 0.0 && frus[i + 0] * aabb.xmin + frus[i + 1] * aabb.ymax + frus[i + 2] * aabb.zmin + frus[i + 3] <= 0.0 && frus[i + 0] * aabb.xmax + frus[i + 1] * aabb.ymax + frus[i + 2] * aabb.zmin + frus[i + 3] <= 0.0 && frus[i + 0] * aabb.xmin + frus[i + 1] * aabb.ymin + frus[i + 2] * aabb.zmax + frus[i + 3] <= 0.0 && diff --git a/source/Menus.cpp b/source/Menus.cpp index 960af33..83ec71e 100644 --- a/source/Menus.cpp +++ b/source/Menus.cpp @@ -276,7 +276,7 @@ void worldmenu(){ vscroll->barpos = 0; chosenWorldName = ""; //查找所有世界存档 - Textures::TEXTURE_RGB* tmb; + Textures::TEXTURE_RGB tmb; long hFile = 0; _finddata_t fileinfo; if ((hFile = _findfirst(string("Worlds\\*").c_str(), &fileinfo)) != -1) @@ -298,9 +298,9 @@ void worldmenu(){ glBindTexture(GL_TEXTURE_2D, thumbnails[thumbnails.size() - 1]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tmb->sizeX, tmb->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, tmb->buffer); - texSizeX[texSizeX.size() - 1] = tmb->sizeX; - texSizeY[texSizeY.size() - 1] = tmb->sizeY; + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tmb.sizeX, tmb.sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, tmb.buffer.get()); + texSizeX[texSizeX.size() - 1] = tmb.sizeX; + texSizeY[texSizeY.size() - 1] = tmb.sizeY; } file.close(); } diff --git a/source/NEWorld.cpp b/source/NEWorld.cpp index 3927d40..b9463b2 100644 --- a/source/NEWorld.cpp +++ b/source/NEWorld.cpp @@ -85,7 +85,7 @@ int selbx, selby, selbz, selcx, selcy, selcz; 和我的不一样,把这几行注释掉吧。。。) ======================================= 等等不对啊!!!明明都改成c++了。。。还说是FB。。。 - VC++编译器应该不会在这儿报错吧23333333 + C++编译器应该不会在这儿报错吧23333333 #endif //============================== Main Program ================================// @@ -999,7 +999,7 @@ void drawMain() { //删除已卸载区块的VBO if (world::vbuffersShouldDelete.size() > 0){ - glDeleteBuffersARB(world::vbuffersShouldDelete.size(), &*world::vbuffersShouldDelete.begin()); + glDeleteBuffersARB(world::vbuffersShouldDelete.size(), world::vbuffersShouldDelete.data()); world::vbuffersShouldDelete.clear(); } @@ -1060,24 +1060,6 @@ void drawMain() { glTranslated(-selx + xpos, -sely + ypos, -selz + zpos); } - /* - Hitbox::AABB t; - t.xmin = world::ciArray.originX * 16; - t.xmax = world::ciArray.originX * 16 + world::ciArray.size * 16; - t.ymin = world::ciArray.originY * 16; - t.ymax = world::ciArray.originY * 16 + world::ciArray.size * 16; - t.zmin = world::ciArray.originZ * 16; - t.zmax = world::ciArray.originZ * 16 + world::ciArray.size * 16; - - Hitbox::AABB t2; - t2.xmin = world::HMap.originX; - t2.xmax = world::HMap.originX + world::HMap.size; - t2.ymin = 36.0; - t2.ymax = 36.0; - t2.zmin = world::HMap.originZ; - t2.zmax = world::HMap.originZ + world::HMap.size; - */ - MutexUnlock(Mutex); glLoadIdentity(); @@ -1117,10 +1099,6 @@ void drawMain() { glRotated(player::lookupdown, 1, 0, 0); glRotated(360.0 - player::heading, 0, 1, 0); glTranslated(-xpos, -ypos, -zpos); - //glDisable(GL_TEXTURE_2D); - //glEnableClientState(GL_VERTEX_ARRAY); - //drawCloud(player::xpos, player::zpos); - //glDisableClientState(GL_VERTEX_ARRAY); glEnable(GL_TEXTURE_2D); glEnable(GL_CULL_FACE); @@ -1174,7 +1152,7 @@ void drawMain() { //屏幕刷新,千万别删,后果自负!!! //====refresh====// MutexUnlock(Mutex); - glFinish(); + //glFinish(); glfwSwapBuffers(MainWindow); glfwPollEvents(); MutexLock(Mutex); @@ -1731,10 +1709,9 @@ void saveScreenshot(int x, int y, int w, int h, string filename){ while (h % 4 != 0){ h -= 1; } scrBuffer.sizeX = w; scrBuffer.sizeY = h; - scrBuffer.buffer = (ubyte*)malloc(w*h * 3); - glReadPixels(x, y, w, h, GL_RGB, GL_UNSIGNED_BYTE, scrBuffer.buffer); + scrBuffer.buffer = unique_ptr((ubyte*)malloc(w*h * 3)); + glReadPixels(x, y, w, h, GL_RGB, GL_UNSIGNED_BYTE, scrBuffer.buffer.get()); Textures::SaveRGBImage(filename, scrBuffer); - free(scrBuffer.buffer); } void createThumbnail(){ diff --git a/source/TextRenderer.cpp b/source/TextRenderer.cpp index cb61254..6109e01 100644 --- a/source/TextRenderer.cpp +++ b/source/TextRenderer.cpp @@ -6,7 +6,7 @@ namespace TextRenderer{ unsigned int Font; int gloop; int ww, wh; - float r=0.0f, g=0.0f, b=0.0f, a=1.0f; + float r = 0.0f, g = 0.0f, b = 0.0f, a = 1.0f; unsigned int unicodeTex[256]; bool unicodeTexAval[256]; bool useUnicodeASCIIFont; diff --git a/source/Textures.cpp b/source/Textures.cpp index 4ebc855..ceeb7fa 100644 --- a/source/Textures.cpp +++ b/source/Textures.cpp @@ -1,23 +1,24 @@ #include "Textures.h" +#include int BLOCKTEXTURE_SIZE, BLOCKTEXTURE_UNITSIZE, BLOCKTEXTURE_UNITS; -namespace Textures{ +namespace Textures { - void Init(){ + void Init() { BLOCKTEXTURE_SIZE = 256; BLOCKTEXTURE_UNITSIZE = 32; BLOCKTEXTURE_UNITS = 8; } - - ubyte getTextureIndex(block blockname, ubyte side){ - switch (blockname){ + + ubyte getTextureIndex(block blockname, ubyte side) { + switch (blockname) { case blocks::AIR: return AIR; case blocks::ROCK: return ROCK; case blocks::GRASS: - switch (side){ + switch (side) { case 1: return GRASS_TOP; case 2: @@ -32,7 +33,7 @@ namespace Textures{ case blocks::PLANK: return PLANK; case blocks::WOOD: - switch (side){ + switch (side) { case 1: return WOOD_TOP; case 2: @@ -66,81 +67,81 @@ namespace Textures{ return UNKNOWN; } } - - double getTexcoordX(block iblock, ubyte side){ + + double getTexcoordX(block iblock, ubyte side) { //return ((getTextureIndex(iblock, side) - 1) % (BLOCKTEXTURE_SIZE / BLOCKTEXTURE_UNITSIZE))*(BLOCKTEXTURE_UNITSIZE / (double)BLOCKTEXTURE_SIZE); return ((getTextureIndex(iblock, side) - 1) & 7) / 8.0; } - double getTexcoordY(block iblock, ubyte side){ + double getTexcoordY(block iblock, ubyte side) { //return (int((getTextureIndex(iblock, side) - 1) / (BLOCKTEXTURE_SIZE / (double)BLOCKTEXTURE_UNITSIZE)))*(BLOCKTEXTURE_UNITSIZE / (double)BLOCKTEXTURE_SIZE); return ((getTextureIndex(iblock, side) - 1) >> 3) / 8.0; } - TEXTURE_RGB* LoadRGBImage(string Filename){ + TEXTURE_RGB LoadRGBImage(string Filename) { unsigned char col[3]; unsigned int ind = 0; - TEXTURE_RGB* bitmap = new TEXTURE_RGB; //返回位图 - bitmap->buffer = nullptr; bitmap->sizeX = bitmap->sizeY = 0; + TEXTURE_RGB bitmap; //返回位图 + bitmap.buffer = nullptr; bitmap.sizeX = bitmap.sizeY = 0; std::ifstream bmpfile(Filename, std::ios::binary | std::ios::in); //位图文件(二进制) - if (!bmpfile.is_open()){ + if (!bmpfile.is_open()) { printf("[console][Warning] Cannot load %s\n", Filename.c_str()); - return nullptr; + return TEXTURE_RGB(); } BITMAPINFOHEADER bih; //各种关于位图的参数 BITMAPFILEHEADER bfh; //各种关于文件的参数 - //开始读取 + //开始读取 bmpfile.read((char*)&bfh, sizeof(BITMAPFILEHEADER)); bmpfile.read((char*)&bih, sizeof(BITMAPINFOHEADER)); - bitmap->sizeX = bih.biWidth; - bitmap->sizeY = bih.biHeight; - bitmap->buffer = new unsigned char[(unsigned int)bitmap->sizeX * bitmap->sizeY * 3]; - for (int i = 0; i < bitmap->sizeX * bitmap->sizeY; i++){ + bitmap.sizeX = bih.biWidth; + bitmap.sizeY = bih.biHeight; + bitmap.buffer = unique_ptr(new unsigned char[bitmap.sizeX * bitmap.sizeY * 3]); + for (unsigned int i = 0; i < bitmap.sizeX * bitmap.sizeY; i++) { //把BGR格式转换为RGB格式 bmpfile.read((char*)col, 3); - bitmap->buffer[ind++] = col[2]; //R - bitmap->buffer[ind++] = col[1]; //G - bitmap->buffer[ind++] = col[0]; //B + bitmap.buffer.get()[ind++] = col[2]; //R + bitmap.buffer.get()[ind++] = col[1]; //G + bitmap.buffer.get()[ind++] = col[0]; //B } bmpfile.close(); return bitmap; } - TEXTURE_RGBA* LoadRGBAImage(string Filename, string MkFilename){ + TEXTURE_RGBA LoadRGBAImage(string Filename, string MkFilename) { unsigned char col[3]; unsigned int ind = 0; - TEXTURE_RGBA* bitmap = new TEXTURE_RGBA; //返回位图 - bitmap->buffer = nullptr; bitmap->sizeX = bitmap->sizeY = 0; + TEXTURE_RGBA bitmap; //返回位图 + bitmap.buffer = nullptr; bitmap.sizeX = bitmap.sizeY = 0; std::ifstream bmpfile(Filename, std::ios::binary | std::ios::in); //位图文件(二进制) std::ifstream maskfile(MkFilename, std::ios::binary | std::ios::in); //遮罩位图文件(二进制) - if (!bmpfile.is_open()){ + if (!bmpfile.is_open()) { printf("[console][Warning] Cannot load %s\n", Filename.c_str()); - return nullptr; + return TEXTURE_RGBA(); } BITMAPFILEHEADER bfh; //各种关于文件的参数 BITMAPINFOHEADER bih; //各种关于位图的参数 - //开始读取 + //开始读取 maskfile.read((char*)&bfh, sizeof(BITMAPFILEHEADER)); //这两个是占位mask文件的 maskfile.read((char*)&bih, sizeof(BITMAPINFOHEADER)); //到了后面mask可以直接从颜色部分开始读取 bmpfile.read((char*)&bfh, sizeof(BITMAPFILEHEADER)); //真正的info以这个bmp文件为准 bmpfile.read((char*)&bih, sizeof(BITMAPINFOHEADER)); //它将覆盖之前从mask文件读出来的info数据 - bitmap->sizeX = bih.biWidth; - bitmap->sizeY = bih.biHeight; - bitmap->buffer = new unsigned char[(unsigned int)bitmap->sizeX * bitmap->sizeY * 4]; + bitmap.sizeX = bih.biWidth; + bitmap.sizeY = bih.biHeight; + bitmap.buffer = unique_ptr(new unsigned char[bitmap.sizeX * bitmap.sizeY * 4]); bool noMaskFile = MkFilename == ""; - for (int i = 0; i < bitmap->sizeX * bitmap->sizeY; i++){ + for (unsigned int i = 0; i < bitmap.sizeX * bitmap.sizeY; i++) { //把BGR格式转换为RGB格式 bmpfile.read((char*)col, 3); - bitmap->buffer[ind++] = col[2]; //R - bitmap->buffer[ind++] = col[1]; //G - bitmap->buffer[ind++] = col[0]; //B - if (noMaskFile){ - bitmap->buffer[ind++] = 255; + bitmap.buffer.get()[ind++] = col[2]; //R + bitmap.buffer.get()[ind++] = col[1]; //G + bitmap.buffer.get()[ind++] = col[0]; //B + if (noMaskFile) { + bitmap.buffer.get()[ind++] = 255; } - else{ + else { //将遮罩图的红色通道反相作为Alpha通道 maskfile.read((char*)col, 3); - bitmap->buffer[ind++] = 255u - col[2]; //A + bitmap.buffer.get()[ind++] = 255 - col[2]; //A } } bmpfile.close(); @@ -148,71 +149,48 @@ namespace Textures{ return bitmap; } - TextureID LoadRGBTexture(string Filename){ - TextureID ret; - TEXTURE_RGB* image = LoadRGBImage(Filename); - glGenTextures(1, &ret); - glBindTexture(GL_TEXTURE_2D, ret); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, (int)log(BLOCKTEXTURE_UNITSIZE)); - gluBuild2DMipmaps(GL_TEXTURE_2D, 3, image->sizeX, image->sizeY, GL_RGB, GL_UNSIGNED_BYTE, image->buffer); - delete[] image->buffer; - delete image; - return ret; - } - - TextureID LoadRGBATexture(string Filename, string MkFilename){ + TextureID LoadRGBTexture(string Filename) { + TEXTURE_RGB image; + ubyte *ip, *tp; TextureID ret; - TEXTURE_RGBA* image = LoadRGBAImage(Filename, MkFilename); + image = LoadRGBImage(Filename); glGenTextures(1, &ret); glBindTexture(GL_TEXTURE_2D, ret); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, (int)log(BLOCKTEXTURE_UNITSIZE)); - gluBuild2DMipmaps(GL_TEXTURE_2D, 4, image->sizeX, image->sizeY, GL_RGBA, GL_UNSIGNED_BYTE, image->buffer); - delete[] image->buffer; - delete image; + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.sizeX, image.sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image.buffer.get()); return ret; } - - TextureID LoadFontTexture(string Filename){ - TEXTURE_RGBA* Tex = new TEXTURE_RGBA; - TEXTURE_RGB* image; + TextureID LoadFontTexture(string Filename) { + TEXTURE_RGBA Texture; + TEXTURE_RGB image; ubyte *ip, *tp; TextureID ret; image = LoadRGBImage(Filename); - Tex->sizeX = image->sizeX; - Tex->sizeY = image->sizeY; - Tex->buffer = new unsigned char[(unsigned int)image->sizeX * image->sizeY * 4]; - if (Tex->buffer == nullptr){ - printf("[Console][Warning] Cannot alloc memory when loading %s\n", Filename.c_str()); + Texture.sizeX = image.sizeX; + Texture.sizeY = image.sizeY; + Texture.buffer = unique_ptr(new unsigned char[image.sizeX * image.sizeY * 4]); + if (Texture.buffer == nullptr) { + printf("[console][Warning] Cannot alloc memory when loading %s\n", Filename.c_str()); return 0; } - ip = image->buffer; - tp = Tex->buffer; - for (int i = 0; i < image->sizeX*image->sizeY; i++){ + ip = image.buffer.get(); + tp = Texture.buffer.get(); + for (unsigned int i = 0; i != image.sizeX*image.sizeY; i++) { *tp = 255; tp++; *tp = 255; tp++; *tp = 255; tp++; - *tp = 255u - *ip; - tp++; ip += 3; + *tp = 255 - *ip; tp++; ip += 3; } glGenTextures(1, &ret); glBindTexture(GL_TEXTURE_2D, ret); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Tex->sizeX, Tex->sizeY, 0, GL_RGBA, GL_UNSIGNED_BYTE, Tex->buffer); - delete[] Tex->buffer; - delete Tex; - delete[] image->buffer; - delete image; + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Texture.sizeX, Texture.sizeY, 0, GL_RGBA, GL_UNSIGNED_BYTE, Texture.buffer.get()); return ret; } - void SaveRGBImage(string filename, TEXTURE_RGB& image){ + void SaveRGBImage(string filename, TEXTURE_RGB& image) { BITMAPFILEHEADER bitmapfileheader; BITMAPINFOHEADER bitmapinfoheader; bitmapfileheader.bfType = BITMAP_ID; @@ -236,8 +214,8 @@ namespace Textures{ std::ofstream ofs(filename, std::ios::out | std::ios::binary); ofs.write((char*)&bitmapfileheader, sizeof(bitmapfileheader)); ofs.write((char*)&bitmapinfoheader, sizeof(bitmapinfoheader)); - p = image.buffer; - for (int index = 0; index < image.sizeX*image.sizeY; index++){ + p = image.buffer.get(); + for (unsigned int index = 0; index != image.sizeX*image.sizeY; index++) { r = *p; p += 1; g = *p; p += 1; b = *p; p += 1; @@ -248,4 +226,17 @@ namespace Textures{ ofs.close(); } + TextureID LoadRGBATexture(string Filename, string MkFilename) { + TextureID ret; + TEXTURE_RGBA image = LoadRGBAImage(Filename, MkFilename); + glGenTextures(1, &ret); + glBindTexture(GL_TEXTURE_2D, ret); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, log(BLOCKTEXTURE_UNITSIZE)); + gluBuild2DMipmaps(GL_TEXTURE_2D, 4, image.sizeX, image.sizeY, GL_RGBA, GL_UNSIGNED_BYTE, image.buffer.get()); + return ret; + } + } \ No newline at end of file diff --git a/source/Textures.h b/source/Textures.h index dcf6dd4..9e51970 100644 --- a/source/Textures.h +++ b/source/Textures.h @@ -9,16 +9,16 @@ namespace Textures{ #pragma pack(push) #pragma pack(1) - struct TEXTURE_RGB{ - int sizeX; - int sizeY; - ubyte* buffer; + struct TEXTURE_RGB { + unsigned int sizeX; + unsigned int sizeY; + unique_ptr buffer; }; - struct TEXTURE_RGBA{ - int sizeX; - int sizeY; - ubyte* buffer; + struct TEXTURE_RGBA { + unsigned int sizeX; + unsigned int sizeY; + unique_ptr buffer; }; struct BITMAPINFOHEADER { @@ -27,7 +27,7 @@ namespace Textures{ int biCompression, biSizeImage, biXPelsPerMeter, biYPelsPerMeter, biClrUsed, biClrImportant; }; - struct BITMAPFILEHEADER{ + struct BITMAPFILEHEADER { short bfType; int bfSize; short bfReserved1, bfReserved2; @@ -35,20 +35,29 @@ namespace Textures{ }; #pragma pack(pop) - enum{ + enum { AIR, ROCK, GRASS_TOP, GRASS_SIDE, DIRT, STONE, PLANK, WOOD_TOP, WOOD_SIDE, BEDROCK, LEAF, GLASS, WATER, LAVA, GLOWSTONE, SAND, CEMENT, ICE, COAL, IRON, UNKNOWN }; void Init(); + ubyte getTextureIndex(block blockname, ubyte side); + double getTexcoordX(block iblock, ubyte side); + double getTexcoordY(block iblock, ubyte side); - TEXTURE_RGB* LoadRGBImage(string Filename); - TEXTURE_RGBA* LoadRGBAImage(string Filename, string MkFilename); + + TEXTURE_RGB LoadRGBImage(string Filename); + + TEXTURE_RGBA LoadRGBAImage(string Filename, string MkFilename); + TextureID LoadRGBTexture(string Filename); - TextureID LoadRGBATexture(string Filename, string MkFilename); + TextureID LoadFontTexture(string Filename); + + TextureID LoadRGBATexture(string Filename, string MkFilename); + void SaveRGBImage(string filename, TEXTURE_RGB& image); } diff --git a/source/World.cpp b/source/World.cpp index e7deec2..9f12ebd 100644 --- a/source/World.cpp +++ b/source/World.cpp @@ -182,7 +182,9 @@ namespace world{ loadedChunks += cc; if (loadedChunks > chunkArraySize) { - chunkArraySize += 1024; + if (chunkArraySize < 1024) chunkArraySize = 1024; + else chunkArraySize *= 2; + while (chunkArraySize < loadedChunks) chunkArraySize *= 2; chunks = (chunk*)realloc(chunks, chunkArraySize * sizeof(chunk)); if (chunks == nullptr && loadedChunks != 0) { printf("[Console][Error]"); @@ -199,24 +201,11 @@ namespace world{ void ReduceChunkArray(int cc){ loadedChunks -= cc; - if (chunkArraySize - 1024 > loadedChunks){ - chunkArraySize -= 1024; - chunks = (chunk*)realloc(chunks, chunkArraySize * sizeof(chunk)); - if (chunks == nullptr && loadedChunks != 0) { - printf("[Console][Error]"); - printf("Chunk Array reducing error.\n"); - saveAllChunks(); - destroyAllChunks(); - glfwTerminate(); - exit(0); - } - } } - void renderblock(int x, int y, int z, int chunkindex) { + void renderblock(int x, int y, int z, chunk* chunkptr) { - chunk* chunkptr = &chunks[chunkindex]; double colors, color1, color2, color3, color4, tcx, tcy, size, EPS = 0.0; int cx = chunkptr->cx, cy = chunkptr->cy, cz = chunkptr->cz; int gx = cx * 16 + x, gy = cy * 16 + y, gz = cz * 16 + z; @@ -238,7 +227,7 @@ namespace world{ size = 1 / 8.0f - EPS; - if (blk[0] == blocks::GRASS && getblock(gx, gy - 1, gz + 1, blocks::ROCK, chunkindex) == blocks::GRASS) { + if (blk[0] == blocks::GRASS && getblock(gx, gy - 1, gz + 1, blocks::ROCK, chunkptr) == blocks::GRASS) { tcx = Textures::getTexcoordX(blk[0], 1) + EPS; tcy = Textures::getTexcoordY(blk[0], 1) + EPS; } @@ -282,7 +271,7 @@ namespace world{ } - if (blk[0] == blocks::GRASS && getblock(gx, gy - 1, gz - 1, blocks::ROCK, chunkindex) == blocks::GRASS) { + if (blk[0] == blocks::GRASS && getblock(gx, gy - 1, gz - 1, blocks::ROCK, chunkptr) == blocks::GRASS) { tcx = Textures::getTexcoordX(blk[0], 1) + EPS; tcy = Textures::getTexcoordY(blk[0], 1) + EPS; } @@ -326,7 +315,7 @@ namespace world{ } - if (blk[0] == blocks::GRASS && getblock(gx + 1, gy - 1, gz, blocks::ROCK, chunkindex) == blocks::GRASS) { + if (blk[0] == blocks::GRASS && getblock(gx + 1, gy - 1, gz, blocks::ROCK, chunkptr) == blocks::GRASS) { tcx = Textures::getTexcoordX(blk[0], 1) + EPS; tcy = Textures::getTexcoordY(blk[0], 1) + EPS; } @@ -369,7 +358,7 @@ namespace world{ } - if (blk[0] == blocks::GRASS && getblock(gx - 1, gy - 1, gz, blocks::ROCK, chunkindex) == blocks::GRASS) { + if (blk[0] == blocks::GRASS && getblock(gx - 1, gy - 1, gz, blocks::ROCK, chunkptr) == blocks::GRASS) { tcx = Textures::getTexcoordX(blk[0], 1) + EPS; tcy = Textures::getTexcoordY(blk[0], 1) + EPS; } @@ -630,15 +619,15 @@ namespace world{ } } - block getblock(int x, int y, int z, block mask, int cindex){ + block getblock(int x, int y, int z, block mask, chunk* cptr){ //获取XYZ的方块 int cx, cy, cz; cx = getchunkpos(x);cy = getchunkpos(y);cz = getchunkpos(z); if (chunkOutOfBound(cx, cy, cz))return blocks::AIR; int bx, by, bz; bx = getblockpos(x);by = getblockpos(y);bz = getblockpos(z); - if (cindex != -1 && cx == chunks[cindex].cx && cy == chunks[cindex].cy && cz == chunks[cindex].cz){ - return chunks[cindex].getblock(bx, by, bz); + if (cptr != nullptr && cx == cptr->cx && cy == cptr->cy && cz == cptr->cz){ + return cptr->getblock(bx, by, bz); } int ci = getChunkIndex(cx, cy, cz); if (ci != -1)return chunks[ci].getblock(bx, by, bz); diff --git a/source/World.h b/source/World.h index 03ba360..ac34750 100644 --- a/source/World.h +++ b/source/World.h @@ -55,9 +55,9 @@ namespace world{ vector getHitboxes(Hitbox::AABB box); bool inWater(Hitbox::AABB box); - void renderblock(int x, int y, int z, int chunkindex); + void renderblock(int x, int y, int z, chunk* chunkptr); void updateblock(int x, int y, int z, bool blockchanged); - block getblock(int x, int y, int z, block mask = blocks::AIR, int cindex = -1); + block getblock(int x, int y, int z, block mask = blocks::AIR, chunk* cptr = nullptr); brightness getbrightness(int x, int y, int z, int cindex = -1); void setblock(int x, int y, int z, block Block); void setbrightness(int x, int y, int z, brightness Brightness); diff --git a/source/WorldGen.cpp b/source/WorldGen.cpp index 274cbf4..f219199 100644 --- a/source/WorldGen.cpp +++ b/source/WorldGen.cpp @@ -13,7 +13,7 @@ namespace WorldGen{ void perlinNoiseInit(int mapseed){ srand(mapseed); for (int i = 0; i < 256; i++){ - perm[i] = (double)(rnd() * 256.0); + perm[i] = rnd() * 256.0; } seed = mapseed; } diff --git a/source/stdinclude.h b/source/stdinclude.h index 03af8d0..01cea3e 100644 --- a/source/stdinclude.h +++ b/source/stdinclude.h @@ -26,6 +26,7 @@ using std::string; using std::vector; using std::pair; +using std::unique_ptr; using std::cout; using std::endl;