From 6de42fdbc74955f1111865633e18fcd2c46167e2 Mon Sep 17 00:00:00 2001 From: "Ben Russell (300178622)" Date: Fri, 26 Apr 2013 11:34:46 +1200 Subject: [PATCH] [0.0-42] gl_expand_textures is available if your gfx chip is old --- clsave/config.json | 1 + docs/setup_json.txt | 4 ++++ include/common.h | 3 ++- pkg/base/version.lua | 7 +++--- src/gl/render_img.c | 55 ++++++++++++++++++++++++++++++++++++++------ src/img.c | 22 ++++++++++++++---- src/lua.c | 5 ++++ src/lua_image.h | 27 ++++++++++++++++++---- src/main.c | 1 + zipdist.sh | 2 +- 10 files changed, 106 insertions(+), 21 deletions(-) diff --git a/clsave/config.json b/clsave/config.json index 10cdb99..040a379 100644 --- a/clsave/config.json +++ b/clsave/config.json @@ -15,6 +15,7 @@ "antialiasinglevel": 0, "smoothlighting": true, + "gl_expand_textures": false, "gl_chunk_size": 16, "gl_visible_chunks": 49, "gl_chunks_tesselated_per_frame": 2, diff --git a/docs/setup_json.txt b/docs/setup_json.txt index 5a01ece..249ef44 100644 --- a/docs/setup_json.txt +++ b/docs/setup_json.txt @@ -37,6 +37,10 @@ clsave/config.json: shadows between cubes. Disable for a faster FPS. + "gl_expand_textures" expands textures to powers of 2 in OpenGL mode + This is for backwards-compatibility with old graphics cards. + Disable this unless all the text is white or something. + "gl_chunk_size" sets the size of the map chunks in OpenGL mode The bigger chunks are, the more time it will take to rebuild them when modified. diff --git a/include/common.h b/include/common.h index 6b407a6..73b9c0b 100644 --- a/include/common.h +++ b/include/common.h @@ -19,7 +19,7 @@ #define VERSION_X 0 #define VERSION_Y 0 #define VERSION_A 0 -#define VERSION_Z 41 +#define VERSION_Z 42 // Remember to bump "Z" basically every time you change the engine! // Remember to bump the version in Lua too! // Remember to document API changes in a new version! @@ -436,6 +436,7 @@ extern int screen_cubeshift; extern int screen_fullscreen; extern int screen_antialiasing_level; extern int screen_smooth_lighting; +extern int gl_expand_textures; extern int gl_use_vbo; extern int gl_chunk_size; extern int gl_visible_chunks; diff --git a/pkg/base/version.lua b/pkg/base/version.lua index d0121be..231730e 100644 --- a/pkg/base/version.lua +++ b/pkg/base/version.lua @@ -16,9 +16,9 @@ ]] VERSION_ENGINE = { - cmp={0,0,0,0,41}, - num=41, - str="0.0-41", + cmp={0,0,0,0,42}, + num=42, + str="0.0-42", } VERSION_BUGS = { @@ -63,5 +63,6 @@ VERSION_BUGS = { {intro=38, fix=nil, msg="[OpenGL] Preliminary stutter-reduced rendering (WIP)"}, {intro=nil, fix=40, msg="[OpenGL] Chunks rendering options not supported in game engine config file"}, {intro=nil, fix=41, msg="[OpenGL] option to disable VBOs not available"}, +{intro=nil, fix=42, msg="[OpenGL] lack of support for cards w/o non-power-of-2 texture support"}, } diff --git a/src/gl/render_img.c b/src/gl/render_img.c index dd3cba3..1e41aab 100644 --- a/src/gl/render_img.c +++ b/src/gl/render_img.c @@ -17,6 +17,33 @@ #include "common.h" +void expandtex_gl(int *iw, int *ih) +{ + if(gl_expand_textures) + { + (*iw)--; + (*iw) |= (*iw)>>1; + (*iw) |= (*iw)>>2; + (*iw) |= (*iw)>>4; + (*iw) |= (*iw)>>8; + (*iw) |= (*iw)>>16; + (*iw)++; + + (*ih)--; + (*ih) |= (*ih)>>1; + (*ih) |= (*ih)>>2; + (*ih) |= (*ih)>>4; + (*ih) |= (*ih)>>8; + (*ih) |= (*ih)>>16; + (*ih)++; + + if((*iw) < 64) + *iw = 64; + if((*ih) < 64) + *ih = 64; + } +} + void render_blit_img_toimg(uint32_t *pixels, int width, int height, int pitch, img_t *src, int dx, int dy, int bw, int bh, int sx, int sy, uint32_t color); @@ -24,7 +51,17 @@ void render_blit_img(uint32_t *pixels, int width, int height, int pitch, img_t *src, int dx, int dy, int bw, int bh, int sx, int sy, uint32_t color) { if(pixels != screen->pixels) + { + expandtex_gl(&width, &height); + pitch = width; return render_blit_img_toimg(pixels,width,height,pitch,src,dx,dy,bw,bh,sx,sy,color); + } + + int iw, ih; + iw = src->head.width; + ih = src->head.height; + expandtex_gl(&iw, &ih); + // TODO: cache shit so we don't have to constantly upload the same image over and over again glEnable(GL_TEXTURE_2D); if(src->tex_dirty) @@ -33,7 +70,7 @@ void render_blit_img(uint32_t *pixels, int width, int height, int pitch, glGenTextures(1, &(src->tex)); glBindTexture(GL_TEXTURE_2D, src->tex); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, src->head.width, src->head.height, 0, GL_BGRA, GL_UNSIGNED_BYTE, src->pixels); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, iw, ih, 0, GL_BGRA, GL_UNSIGNED_BYTE, src->pixels); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); @@ -59,10 +96,10 @@ void render_blit_img(uint32_t *pixels, int width, int height, int pitch, float dx2 = dx+bw; float dy2 = dy+bh; - float sx1 = (sx)/(float)src->head.width; - float sx2 = (sx+bw)/(float)src->head.width; - float sy1 = (sy)/(float)src->head.height; - float sy2 = (sy+bh)/(float)src->head.height; + float sx1 = (sx)/(float)iw; + float sx2 = (sx+bw)/(float)iw; + float sy1 = (sy)/(float)ih; + float sy2 = (sy+bh)/(float)ih; glColor4f(((color>>16)&255)/255.0f,((color>>8)&255)/255.0f,((color)&255)/255.0f,((color>>24)&255)/255.0f); glBegin(GL_QUADS); @@ -135,10 +172,14 @@ void render_blit_img_toimg(uint32_t *pixels, int width, int height, int pitch, return; // get pointers + int iw, ih; + iw = src->head.width; + ih = src->head.height; + expandtex_gl(&iw, &ih); uint32_t *ps = src->pixels; - ps = &ps[sx+sy*src->head.width]; + ps = &ps[sx+sy*iw]; uint32_t *pd = &(pixels[dx+dy*pitch]); - int spitch = src->head.width - bw; + int spitch = iw - bw; int dpitch = pitch - bw; //printf("[%i %i] [%i %i] %016llX %016llX %i %i %08X\n" diff --git a/src/img.c b/src/img.c index 4db6535..3a5d77b 100644 --- a/src/img.c +++ b/src/img.c @@ -17,6 +17,10 @@ #include "common.h" +#ifdef USE_OPENGL +void expandtex_gl(int *iw, int *ih); +#endif + uint32_t img_convert_color_to_32(uint32_t v, int bits) { switch(bits) @@ -76,7 +80,14 @@ img_t *img_parse_tga(int len, const char *data) } // allocate + stash - img_t *img = (img_t*)malloc(sizeof(img_t)+4*head.width*head.height); + int iw, ih; + iw = head.width; + ih = head.height; +#ifdef USE_OPENGL + expandtex_gl(&iw, &ih); +#endif + printf("TEX: %i %i\n", iw, ih); + img_t *img = (img_t*)malloc(sizeof(img_t)+4*iw*ih); // TODO: check if NULL img->head = head; img->udtype = UD_IMG; @@ -87,7 +98,7 @@ img_t *img_parse_tga(int len, const char *data) // copy stuff int bplen = ((head.bpp-1)>>3)+1; - int idx = (head.flags & 32 ? 0 : head.height-1)*head.width; + int idx = (head.flags & 32 ? 0 : head.height-1)*iw; for(y = 0; y < head.height; y++) { if(head.imgtype & 8) @@ -128,18 +139,19 @@ img_t *img_parse_tga(int len, const char *data) } } + idx += iw-head.width; if(!(head.flags & 32)) - idx -= 2*head.width; + idx -= 2*iw; } // convert pixels if((head.imgtype&7) == 1) { - for(i = head.width*head.height-1; i >= 0; i--) + for(i = iw*ih-1; i >= 0; i--) img->pixels[i] = palette[(img->pixels[i] + head.cmoffs) % head.cmlen]; //printf("cm %i %i\n", head.cmoffs, head.cmlen); } else { - for(i = head.width*head.height-1; i >= 0; i--) + for(i = iw*ih-1; i >= 0; i--) img->pixels[i] = img_convert_color_to_32(img->pixels[i], head.bpp); } diff --git a/src/lua.c b/src/lua.c index e643747..2d928d6 100644 --- a/src/lua.c +++ b/src/lua.c @@ -347,6 +347,11 @@ int icelua_init(void) if(!lua_isnil(Lc, -1)) gl_use_vbo = v; lua_pop(Lc, 1); + lua_getfield(Lc, -1, "gl_expand_textures"); + v = lua_toboolean(Lc, -1); + if(!lua_isnil(Lc, -1)) gl_expand_textures = v; + lua_pop(Lc, 1); + lua_getfield(Lc, -1, "gl_chunk_size"); v = lua_tointeger(Lc, -1); if(v > 0) gl_chunk_size = v; diff --git a/src/lua_image.h b/src/lua_image.h index a97c3ed..b1debb3 100644 --- a/src/lua_image.h +++ b/src/lua_image.h @@ -14,6 +14,9 @@ You should have received a copy of the GNU General Public License along with Iceball. If not, see . */ +#ifdef USE_OPENGL +void expandtex_gl(int *iw, int *ih); +#endif // client functions int icelua_fn_client_img_blit(lua_State *L) @@ -114,11 +117,16 @@ int icelua_fn_common_img_new(lua_State *L) int w = lua_tointeger(L, 1); int h = lua_tointeger(L, 2); + int iw = w; + int ih = h; +#ifdef USE_OPENGL + expandtex_gl(&iw, &ih); +#endif if(w < 1 || h < 1) return luaL_error(L, "image too small"); - img_t *img = (img_t*)malloc(sizeof(img_t)+(w*h*sizeof(uint32_t))); + img_t *img = (img_t*)malloc(sizeof(img_t)+(iw*ih*sizeof(uint32_t))); if(img == NULL) return luaL_error(L, "could not allocate memory"); @@ -135,7 +143,7 @@ int icelua_fn_common_img_new(lua_State *L) img->head.bpp = 32; img->head.flags = 0x20; - for(i = 0; i < w*h; i++) + for(i = 0; i < iw*ih; i++) img->pixels[i] = 0x00000000; img->udtype = UD_IMG; @@ -161,8 +169,14 @@ int icelua_fn_common_img_pixel_set(lua_State *L) if(x < 0 || y < 0 || x >= img->head.width || y >= img->head.height) return 0; + + int iw = img->head.width; + int ih = img->head.height; +#ifdef USE_OPENGL + expandtex_gl(&iw, &ih); +#endif - img->pixels[y*img->head.width+x] = color; + img->pixels[y*iw+x] = color; #ifdef USE_OPENGL img->tex_dirty = 1; #endif @@ -181,7 +195,12 @@ int icelua_fn_common_img_fill(lua_State *L) return luaL_error(L, "not an image"); uint32_t color = lua_tointeger(L, 2); - for (i=0; i<(img->head.width*img->head.height); i++) + int iw = img->head.width; + int ih = img->head.height; +#ifdef USE_OPENGL + expandtex_gl(&iw, &ih); +#endif + for (i=0; i<(iw*ih); i++) img->pixels[i] = color; #ifdef USE_OPENGL diff --git a/src/main.c b/src/main.c index 6f3bf4f..34db606 100644 --- a/src/main.c +++ b/src/main.c @@ -30,6 +30,7 @@ int screen_cubeshift = 0; int screen_fullscreen = 0; int screen_antialiasing_level = 0; int screen_smooth_lighting = 0; +int gl_expand_textures = 0; int gl_chunk_size = 16; int gl_visible_chunks = 49; int gl_chunks_tesselated_per_frame = 2; diff --git a/zipdist.sh b/zipdist.sh index 06f178e..e3cbfca 100755 --- a/zipdist.sh +++ b/zipdist.sh @@ -1,6 +1,6 @@ #!/bin/sh -export ZIPNAME=nubdist/iceballfornoobs-0.0-41.zip +export ZIPNAME=nubdist/iceballfornoobs-0.0-42.zip #zip -r $ZIPNAME *.dll *.exe *.txt *.bat docs/ \ # pkg/base/*.lua \