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<window.mw && x>=0 && y>=0 && y<window.mh) { window.oldtile[x][y]=window.tile[x][y]; window.tile[x][y].symbol=code; window.tile[x][y].color="white"; window.tile[x][y].backcolor="black"; } }
Win_Draw:
al_color_name(); will help clean up your code, at least.
The problem is that the ALLEGRO_COLOR names aren't the same as traditional ASCII colors. I'll try to see what I can do with it, though.
What's with all the strcmp's in Win_Draw? Make a color map :
ColorMap.hpp
#include <map> #include <string> extern std::map<std::string , ALLEGRO_COLOR> color_map; void InitializeColorMap();
ColorMap.cpp
using std::map; using std::string; map<string , ALLEGRO_COLOR> 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
Other things - Why are you using al_draw_tinted_bitmap_region? Why not just use al_draw_filled_rectangle? And I don't see you actually draw the character anywhere.
Using the std::map will at least give you a binary search, which should be decently faster than strcmp'ing every known color value every iteration of the loop.
Also, why is tile one wider and one taller than window?
As Peter Wang said in the other thread, why not just use ALLEGRO_COLOR directly, instead of storing the color name?
I could not, for the life of me, get ALLEGRO_COLORs to work correctly. But using the code above, that doesn't really matter anymore.
Why not just use al_draw_filled_rectangle? And I don't see you actually draw the character anywhere.
I assume primitives are faster than drawing bitmap regions, then? The character is drawn by the second al_draw_tinted_bitmap_region in Win_Draw.
Anyway, I think the code above helped a bit, but it's still using more CPU than it has any right to.
I assume primitives are faster than drawing bitmap regions, then?
Maybe, maybe not. But al_draw_filled_rectangle should be faster than using al_draw_tinted_bitmap_region, at least I would think so.
How do your draw your characters background using al_draw_tinted_bitmap_region? And why are you tinting it? Don't you want just the colors from the background/foreground? Why not use al_draw_text for the character? Just use a char array[2] and set array[0] to your character and array[1] to '\0'.
About the colors : just add an ALLEGRO_COLOR entry to your TILE class and when your users set the color using a color name, find the ALLEGRO_COLOR from your color map and use that.
Maybe, maybe not. But al_draw_filled_rectangle should be faster than using al_draw_tinted_bitmap_region, at least I would think so.
How do your draw your characters background using al_draw_tinted_bitmap_region? And why are you tinting it? Don't you want just the colors from the background/foreground? Why not use al_draw_text for the character? Just use a char array[2] and set array[0] to your character and array[1] to '\0'.
About the colors : just add an ALLEGRO_COLOR entry to your TILE class and when your users set the color using a color name, find the ALLEGRO_COLOR from your color map and use that.
The ascii table is in a bitmap like this one:
{"name":"3oNIz.png","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/6\/06e4de4fdba7ae5cd41a73426a5d3992.png","w":256,"h":112,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/6\/06e4de4fdba7ae5cd41a73426a5d3992"}
First, the background is alpha'd out. It draws the ASCII character's background using the "blank" character tinted to the correct background color. It then draws the ASCII character over it with the specified foreground color.
Can al_draw_text use unicode characters, or just regular old keyboard characters?
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:
EDIT: nvm
EDIT2: Also, try adding al_hold_bitmap_drawing like I did above (highlighted).
How are are changes occurring on the screen? Are all the ascii constantly changing (like an ascii video player), or only changing periodically?
If the ascii isn't changing every time you draw the screen, you could store a copy to use when you refresh, so you are not needlessly going through this loop to produce the same results.