From: Tobias Gläßer Date: Wed, 5 May 2004 22:04:48 +0000 (+0000) Subject: added draw_stretched capability to surface* and tile* classes and moreover built... X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=361976b834e1e5a266cba08c6ae2c53c503f682d;p=supertux.git added draw_stretched capability to surface* and tile* classes and moreover built a minimap upon it for the leveleditor. Unfortunately it only works correctly in OpenGL mode, therefore it is disabled in software mode. SVN-Revision: 1000 --- diff --git a/src/leveleditor.cpp b/src/leveleditor.cpp index e864c8404..795d26b33 100644 --- a/src/leveleditor.cpp +++ b/src/leveleditor.cpp @@ -128,6 +128,7 @@ struct TileOrObject /* leveleditor internals */ static string_list_type level_subsets; static bool le_level_changed; /* if changes, ask for saving, when quiting*/ +static bool show_minimap; static int pos_x, cursor_x, cursor_y, fire; static int le_level; static LevelEditorWorld le_world; @@ -782,6 +783,41 @@ void le_quit(void) } } +void le_drawminimap() +{ +if(le_current_level == NULL) +return; + +int mini_tile_width; +if(screen->w - 64 > le_current_level->width * 4) +mini_tile_width = 4; +else if(screen->w - 64 > le_current_level->width * 2) +mini_tile_width = 2; +else +mini_tile_width = 1; +int left_offset = (screen->w - 64 - le_current_level->width*mini_tile_width) / 2; + + for (int y = 0; y < 15; ++y) + for (int x = 0; x < le_current_level->width; ++x) + { + + Tile::draw_stretched(left_offset + mini_tile_width*x, y * 4, mini_tile_width , 4, le_current_level->bg_tiles[y][x]); + + Tile::draw_stretched(left_offset + mini_tile_width*x, y * 4, mini_tile_width , 4, le_current_level->ia_tiles[y][x]); + + Tile::draw_stretched(left_offset + mini_tile_width*x, y * 4, mini_tile_width , 4, le_current_level->fg_tiles[y][x]); + + } + +fillrect(left_offset, 0, le_current_level->width*mini_tile_width, 15*4, 200, 200, 200, 128); + +fillrect(left_offset + (pos_x/32)*mini_tile_width, 0, 19*mini_tile_width, 2, 200, 200, 200, 200); +fillrect(left_offset + (pos_x/32)*mini_tile_width, 0, 2, 15*4, 200, 200, 200, 200); +fillrect(left_offset + (pos_x/32)*mini_tile_width + 19*mini_tile_width - 2, 0, 2, 15*4, 200, 200, 200, 200); +fillrect(left_offset + (pos_x/32)*mini_tile_width, 15*4-2, 19*mini_tile_width, 2, 200, 200, 200, 200); + +} + void le_drawinterface() { int x,y; @@ -798,6 +834,9 @@ void le_drawinterface() fillrect(0, y*32, screen->w - 32, 1, 225, 225, 225,255); } } + + if(show_minimap && use_gl) // use_gl because the minimap isn't shown correctly in software mode. Any idea? FIXME Possible reasons: SDL_SoftStretch is a hack itsself || an alpha blitting issue SDL can't handle in software mode + le_drawminimap(); if(le_selection_mode == CURSOR) if(le_current.IsTile()) @@ -1353,22 +1392,28 @@ void le_checkevents() } if(!Menu::current()) { + show_minimap = false; + if(le_move_left_bt->get_state() == BUTTON_PRESSED) { pos_x -= 192; + show_minimap = true; } else if(le_move_left_bt->get_state() == BUTTON_HOVER) { pos_x -= 32; + show_minimap = true; } if(le_move_right_bt->get_state() == BUTTON_PRESSED) { pos_x += 192; + show_minimap = true; } else if(le_move_right_bt->get_state() == BUTTON_HOVER) { pos_x += 32; + show_minimap = true; } } diff --git a/src/texture.cpp b/src/texture.cpp index 3aafe0efa..df33c74ff 100644 --- a/src/texture.cpp +++ b/src/texture.cpp @@ -237,6 +237,16 @@ Surface::draw_part(float sx, float sy, float x, float y, float w, float h, Uint } void +Surface::draw_stretched(float x, float y, int w, int h, Uint8 alpha, bool update) +{ + if (impl) + { + if (impl->draw_stretched(x, y, w, h, alpha, update) == -2) + reload(); + } +} + +void Surface::resize(int w_, int h_) { if (impl) @@ -571,6 +581,41 @@ SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h, (void) update; // avoid warnings return 0; } + +int +SurfaceOpenGL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, bool update) +{ + float pw = power_of_two(int(this->w)); + float ph = power_of_two(int(this->h)); + + glBindTexture(GL_TEXTURE_2D, gl_texture); + + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + glColor4ub(alpha, alpha, alpha, alpha); + + glEnable(GL_TEXTURE_2D); + + + glBegin(GL_QUADS); + glTexCoord2f(0, 0); + glVertex2f(x, y); + glTexCoord2f((float)w / pw, 0); + glVertex2f(sw+x, y); + glTexCoord2f((float)w / pw, (float)h / ph); glVertex2f((float)sw+x, (float)sh+y); + glVertex2f(sw +x, sh+y); + glTexCoord2f(0, (float)h / ph); + glVertex2f(x, sh+y); + glEnd(); + + glDisable(GL_TEXTURE_2D); + glDisable(GL_BLEND); + + (void) update; // avoid warnings + return 0; +} + #endif SurfaceSDL::SurfaceSDL(SDL_Surface* surf, int use_alpha) @@ -680,6 +725,27 @@ SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Ui return ret; } +int +SurfaceSDL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, bool update) +{ + SDL_Rect dest; + + dest.x = (int)x; + dest.y = (int)y; + dest.w = (int)sw; + dest.h = (int)sh; + + if(alpha != 255) + SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha); + + int ret = SDL_SoftStretch(sdl_surface, NULL, screen, &dest); + + if (update == UPDATE) + update_rect(screen, dest.x, dest.y, dest.w, dest.h); + + return ret; +} + SurfaceSDL::~SurfaceSDL() {} diff --git a/src/texture.h b/src/texture.h index 3cd8c2228..53cb3a189 100644 --- a/src/texture.h +++ b/src/texture.h @@ -85,7 +85,8 @@ public: void draw(float x, float y, Uint8 alpha = 255, bool update = false); void draw_bg(Uint8 alpha = 255, bool update = false); void draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha = 255, bool update = false); - void Surface::resize(int w_, int h_); + void draw_stretched(float x, float y, int w, int h, Uint8 alpha, bool update = false); + void resize(int w_, int h_); }; /** Surface implementation, all implementation have to inherit from @@ -107,6 +108,7 @@ public: virtual int draw(float x, float y, Uint8 alpha, bool update) = 0; virtual int draw_bg(Uint8 alpha, bool update) = 0; virtual int draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update) = 0; + virtual int draw_stretched(float x, float y, int w, int h, Uint8 alpha, bool update) = 0; int resize(int w_, int h_); SDL_Surface* get_sdl_surface() const; // @evil@ try to avoid this function @@ -123,6 +125,7 @@ public: int draw(float x, float y, Uint8 alpha, bool update); int draw_bg(Uint8 alpha, bool update); int draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update); + int draw_stretched(float x, float y, int w, int h, Uint8 alpha, bool update); }; #ifndef NOOPENGL @@ -140,6 +143,7 @@ public: int draw(float x, float y, Uint8 alpha, bool update); int draw_bg(Uint8 alpha, bool update); int draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update); + int draw_stretched(float x, float y, int w, int h, Uint8 alpha, bool update); private: void create_gl(SDL_Surface * surf, GLuint * tex); diff --git a/src/tile.cpp b/src/tile.cpp index 5b0a356ec..4e3fa922b 100644 --- a/src/tile.cpp +++ b/src/tile.cpp @@ -207,5 +207,29 @@ Tile::draw(float x, float y, unsigned int c, Uint8 alpha) } } +void +Tile::draw_stretched(float x, float y, int w, int h, unsigned int c, Uint8 alpha) +{ + if (c != 0) + { + Tile* ptile = TileManager::instance()->get(c); + if(ptile) + { + if(ptile->images.size() > 1) + { + ptile->images[( ((global_frame_counter*25) / ptile->anim_speed) % (ptile->images.size()))]->draw_stretched(x,y,w,h, alpha); + } + else if (ptile->images.size() == 1) + { + ptile->images[0]->draw_stretched(x,y, w, h, alpha); + } + else + { + //printf("Tile not dravable %u\n", c); + } + } + } +} + // EOF // diff --git a/src/tile.h b/src/tile.h index 91cedc55f..dfc6a238f 100644 --- a/src/tile.h +++ b/src/tile.h @@ -80,6 +80,7 @@ public: /** Draw a tile on the screen: */ static void draw(float x, float y, unsigned int c, Uint8 alpha = 255); + static void draw_stretched(float x, float y, int w, int h, unsigned int c, Uint8 alpha = 255); }; struct TileGroup