Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » My CPU usage is absurd

This thread is locked; no one can reply to it. rss feed Print
 1   2 
My CPU usage is absurd
Gabriel Campos
Member #12,034
June 2010
avatar

i am not using mappy anymore. I created a file with intenger
level{0, 1, 2, 40, 23, 0, 0 ... and so on. Still consuming cpu. This is annoying.

Kris Asick
Member #1,424
July 2001

Could also be vsync related. If you're not vsyncing, or your GPU drivers are set to force vsync off by default, that could lead to a massive unnecessary waste of CPU power.

--- Kris Asick (Gemini)
--- http://www.pixelships.com

Thomas Fjellstrom
Member #476
June 2000
avatar

If you're not waiting in your main loop, its going to suck up an entire core, which shows up as 50% on a dual core system (as someone previously mentioned).

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Edgar Reynaldo
Member #8,592
May 2007
avatar

i am not using mappy anymore. I created a file with intenger
level{0, 1, 2, 40, 23, 0, 0 ... and so on. Still consuming cpu. This is annoying.

Edgar said:

Like I said before, mappy doesn't use atlases or held drawing, both of which would speed up your drawing.

And are you using atlases or held drawing in your own code? I'm guessing not.

You're gonna have to show the code you're using now. We can't guess what's wrong anymore.

Gabriel Campos
Member #12,034
June 2010
avatar

#SelectExpand
1#include "LevelManager.h" 2 3LevelManager::LevelManager(void) 4{ 5 blockSize = 32; 6} 7 8LevelManager::~LevelManager(void) 9{ 10} 11 12void LevelManager::load(const char *filename) 13{ 14 int state; 15 enum loadState{TILESET, LAYER_BG, LAYER_LV}; 16 std::fstream file(filename); 17 std::vector<int>tempVector; 18 19 if(file.is_open()) 20 { 21 std::string line, value; 22 23 while(!file.eof()) 24 { 25 std::getline(file, line); 26 27 if(line.find("[tileset]") != std::string::npos) 28 { 29 state = TILESET; 30 continue; 31 } 32 33 else if(line.find("[layer_bg]") != std::string::npos) 34 { 35 state = LAYER_BG; 36 continue; 37 } 38 39 else if(line.find("[layer_lv]") != std::string::npos) 40 { 41 state = LAYER_LV; 42 continue; 43 } 44 45 switch(state) 46 { 47 case TILESET: 48 if(line.length() > 0) 49 tileset = al_load_bitmap(line.c_str()); 50 51 al_convert_mask_to_alpha(tileset, al_get_pixel(tileset, 0, 0)); 52 break; 53 54 case LAYER_BG: 55 setLayer_bg(line); 56 break; 57 58 case LAYER_LV: 59 setLayer_lv(line); 60 break; 61 } 62 63 } 64 } 65 else 66 { 67 } 68 file.close(); 69} 70 71void LevelManager::unload() 72{ 73 al_destroy_bitmap(tileset); 74} 75 76void LevelManager::render() 77{ 78 int w = al_get_bitmap_width(tileset) / blockSize; 79 int x, y; 80 81// LAYER BACKGROUND 82 for(int i = 0; i < layer_bg.size(); i++) 83 { 84 for(int j = 0; j < layer_bg[i].size(); j++) 85 { 86 x = ((layer_bg[i][j] % w) * blockSize) - blockSize; 87 y = (layer_bg[i][j] / w) * blockSize; 88 al_draw_bitmap_region(tileset, x, y, blockSize, blockSize, j * blockSize, i * blockSize, NULL); 89 } 90 } 91 92// LAYER LEVEL 93 for(int i = 0; i < layer_lv.size(); i++) 94 { 95 for(int j = 0; j < layer_lv[i].size(); j++) 96 { 97 x = ((layer_lv[i][j] % w) * blockSize) - blockSize; 98 y = (layer_lv[i][j] / w) * blockSize; 99 al_draw_bitmap_region(tileset, x, y, blockSize, blockSize, j * blockSize, i * blockSize, NULL); 100 } 101 } 102} 103 104void LevelManager::setLayer_bg(std::string line) 105{ 106 std::string value; 107 std::stringstream str(line); 108 std::vector<int>tempVector; 109 110 while(!str.eof()) 111 { 112 std::getline(str, value, ','); 113 if(value.length() > 0) 114 tempVector.push_back(atoi(value.c_str())); 115 } 116 layer_bg.push_back(tempVector); 117} 118 119void LevelManager::setLayer_lv(std::string line) 120{ 121 std::string value; 122 std::stringstream str(line); 123 std::vector<int>tempVector; 124 125 while(!str.eof()) 126 { 127 std::getline(str, value, ','); 128 if(value.length() > 0) 129 tempVector.push_back(atoi(value.c_str())); 130 } 131 layer_lv.push_back(tempVector); 132}

Its simple load the map from a txt file and the render function is called every loop to draw the map.

Werwolf696
Member #15,203
June 2013
avatar

Can you post the code that calls your level manager render routine?

Gabriel Campos
Member #12,034
June 2010
avatar

#SelectExpand
1#include "SceneGame.h" 2 3SceneGame::SceneGame() 4{ 5} 6 7SceneGame::~SceneGame() 8{ 9} 10 11void SceneGame::load() 12{ 13 player.load(); 14 levelManager.load("Data/Levels/level00.txt"); 15 //music = al_load_sample("Data/Sounds/01_level.ogg"); 16 //al_play_sample(music, 1, 0, 1, ALLEGRO_PLAYMODE_LOOP, NULL); 17} 18 19void SceneGame::unload() 20{ 21 player.unload(); 22 levelManager.unload(); 23 //al_destroy_sample(music); 24} 25 26void SceneGame::input(ALLEGRO_EVENT *event) 27{ 28 if(inputManager.isKeyPressed(event, ALLEGRO_KEY_Z)) 29 Engine::instance().changeScene(new SceneTitle()); 30 31 player.input(event); 32} 33 34void SceneGame::logic() 35{ 36 player.logic(); 37} 38 39void SceneGame::render() 40{ 41 levelManager.render(); 42 player.render(); 43}

taronĀ 
Member #10,584
January 2009
avatar

For one you're drawing the whole map completely regardless of how much of it is visible, which might be a problem if your map is really large, but I'll assume it's a small test map.

Before drawing the tilemap call al_hold_bitmap_drawing(true) and after drawing call al_hold_bitmap_drawing(false).

The manual gives a more in-depth explanation of why and when to use it.

 1   2 


Go to: