Allegro.cc Forums » Programming Questions » Optimizing Draw Functions

 Gatleos Member #12,534 January 2011 So. My ascii library's draw functions are EVIL CPU-SUCKERS. There are two parts: a Draw_Char function that doesn't draw anything to the screen but updates the symbol at a certain location, and Win_Draw, which updates the given Window by redrawing every character in it. Obviously running them every time the game loops isn't the best idea, but does anyone have any tips on how to best optimize these things to prevent lag?Draw_Char: ```void Draw_Char(int code,Window& window,int x,int y) { if(x=0 && y>=0 && y255) 75code=63; 76int backcode=219; 77int row = code / colnum; 78int backrow = backcode / colnum; 79while(code>=colnum) 80code-=colnum; 81while(backcode>=colnum) 82backcode-=colnum; 83if(i
 Edgar Reynaldo Member #8,592 May 2007 What's with all the strcmp's in Win_Draw? Make a color map :ColorMap.hpp ```#include #include extern std::map color_map; void InitializeColorMap(); ``` ColorMap.cpp ```using std::map; using std::string; map color_map; void InitializeColorMap() { color_map.clear(); color_map["black"] = al_map_rgba(0,0,0,255); color_map["lightgrey"] = al_map_rgba(192,192,192,255); // repeat for all of your named colors } ``` Win_Draw.cpp #SelectExpand 1#include "ColorMap.hpp" 2 3void Win_Draw(Window& windowsource) 4{ 5for(int j=0;j<=windowsource.h;++j) 6for(int i=0;i<=windowsource.w;++i) 7{ 8const char* wcolor = windowsource.tile[i][j].color; 9const char* wbcolor = windowsource.tile[i][j].backcolor; 10ALLEGRO_COLOR color=al_map_rgba(0,0,0,0); 11if (color_map.find(wcolor) != color_map.end()) { 12 color = color_map[wcolor]; 13} 14ALLEGRO_COLOR backcolor=al_map_rgba(0,0,0,0); 15if (color_map.find(wbcolor) != color_map.end()) { 16 color = color_map[wbcolor]; 17} 18 19 20 21int code=windowsource.tile[i][j].symbol; 22if(code<0 || code>255) 23code=63; 24int backcode=219; 25int row = code / colnum; 26int backrow = backcode / colnum; 27while(code>=colnum) 28code-=colnum; 29while(backcode>=colnum) 30backcode-=colnum; 31if(i
 SiegeLord Member #7,827 October 2006 Gatleos said: But using the code above, that doesn't really matter anymore. Yeah, it kind of does. std::map is still a lot slower than doing absolutely nothing would be.Here's how it'd look:```void Draw_Char(int code,Window& window,int x,int y) { if(x=0 && y>=0 && y255) 12code=63; 13int backcode=219; 14int row = code / colnum; 15int backrow = backcode / colnum; 16while(code>=colnum) 17code-=colnum; 18while(backcode>=colnum) 19backcode-=colnum; 20if(i