Note that there are some explanatory texts on larger screens.

plurals
  1. POMy program displays empty surface, where it needs to display a map
    primarykey
    data
    text
    <p>I have a problem with SDL. As you see, I have 3 files: include.h, map.h, source.cpp.</p> <p>In include.h I included all libraries. In map.h I wrote a class: getTileID reads a picture and slices it to tiles, getTilePosition reads .txt file, drawMap function makes a surface assigning sliced surface tiles to id values from text file.</p> <p>Then, in source.cpp I initiliaze SDL, SetVideoMode, and create a class A. After that, I call class A functions. After calling it, I A.mapSurface[0] variable to screen and flip it. </p> <p>Nothing happens. Screen loads, and, as I think it flips with empty surface, but it needs to display mapSurface variable.</p> <p>Please help.</p> <pre><code>//include.h //---------------------------------------------------------------------------------------------------- #include "headers\SDL.h" #include "headers\SDL_image.h" //---------------------------------------------------------------------------------------------------- #pragma comment(lib, "SDL.lib") #pragma comment(lib, "SDLmain.lib") #pragma comment(lib, "SDL_image.lib") //---------------------------------------------------------------------------------------------------- #include &lt;string&gt; #include &lt;fstream&gt; //---------------------------------------------------------------------------------------------------- #include "map.h" </code></pre> <p>-</p> <pre><code>//map.h //---------------------------------------------------------------------------------------------------- #pragma once //#include "include.h" //---------------------------------------------------------------------------------------------------- class map { public: //---------------------------------------------------------------------------------------------------- map(void); ~map(void); void getTileID(const char* mapFile); void getTilePosition(std::string mapFile); void drawMap(); //---------------------------------------------------------------------------------------------------- static const short int TILEMAP_WIDTH = 24; static const short int TILEMAP_HEIGHT = 24; static const short int TILE_WIDTH = 32; static const short int TILE_HEIGHT = 32; SDL_Surface* tileID[1025]; short int mapWidth; short int mapHeight; short int mapID[2][500][500]; short int graphicLayer; SDL_Surface* mapSurface[5]; }; //---------------------------------------------------------------------------------------------------- map::map(void) { } map::~map(void) { } //---------------------------------------------------------------------------------------------------- void map::getTileID(const char* mapFile) { IMG_Init(IMG_INIT_PNG); SDL_Surface *tileMap; tileMap = IMG_Load(mapFile); if(tileMap == NULL) { exit(1); } for(short int i = 0; i &lt; map::TILEMAP_WIDTH * map::TILEMAP_HEIGHT + 1; i++) { map::tileID[i] = (SDL_CreateRGBSurface(SDL_HWSURFACE | SDL_SRCALPHA, map::TILE_WIDTH, map::TILE_HEIGHT, 32, 0, 0, 0, 0)); if(map::tileID[i] == NULL) { exit(2); } } tileID[0] = NULL; short int id = 1; for(short int c = 0; c &lt; map::TILEMAP_WIDTH; c++) { for(short int r = 0; r &lt; map::TILEMAP_HEIGHT; r++) { short int sliceX = c * map::TILE_WIDTH; short int sliceY = r * map::TILE_HEIGHT; SDL_Rect srcRect; srcRect.x = sliceX; srcRect.y = sliceY; srcRect.w = map::TILE_WIDTH; srcRect.h = map::TILE_HEIGHT; SDL_Rect dstRect; dstRect.x = 0; dstRect.y = 0; dstRect.h = 32; dstRect.w = 32; if(SDL_BlitSurface(tileMap, &amp;srcRect, map::tileID[id], &amp;dstRect) != NULL) exit(3); id++; } } SDL_FreeSurface(tileMap); IMG_Quit(); } //---------------------------------------------------------------------------------------------------- void map::getTilePosition(std::string mapFile) { std::string tag, objType[10]; short int l = 0; float objX[10], objY[10], objWidth[10], objHeight[10]; std::ifstream data(mapFile); while(!data.eof()) { getline(data, tag); if(tag == "[header]") { data.ignore(256, '='); data &gt;&gt; map::mapWidth; data.ignore(256, '='); data &gt;&gt; map::mapHeight; data.ignore(256, '\n'); } map::graphicLayer = 0; if(tag == "[layer]") { data.ignore(256, '\n'); data.ignore(256, '\n'); for(short int c = 0; c &lt; map::mapHeight; c++) { for(short int r = 0; r &lt; map::mapWidth; r++) { data &gt;&gt; map::mapID[map::graphicLayer][c][r]; } } map::graphicLayer++; data.ignore(256, '\n'); } if(tag.substr(0, 7) == "[object") { objType[l] = tag.substr(8, tag.size() - 9); data.ignore(256, '\n'); data.ignore(256, '\n'); data.ignore(256, '='); data &gt;&gt; objX[l] &gt;&gt; objY[l] &gt;&gt; objWidth[l] &gt;&gt; objHeight[l]; l++; data.ignore(256, '\n'); } } data.close(); } //---------------------------------------------------------------------------------------------------- void map::drawMap() { map::mapSurface[0] = (SDL_CreateRGBSurface(SDL_HWSURFACE | SDL_SRCALPHA, 3200 ,3200 , 32, 255, 255, 255, 0)); for(short int gl = 0; gl &lt; map::graphicLayer; gl++) { for(short int c = 0; c &lt; map::mapWidth; c++) { for(short int r = 0; r &lt; map::mapHeight; r++) { for(short int id = 0; id &lt; map::TILEMAP_WIDTH * map::TILEMAP_HEIGHT; id++) { if(map::mapID[gl][c][r] == id) { SDL_Rect dstRect; dstRect.x = c * map::TILE_WIDTH; dstRect.y = r * map::TILE_HEIGHT; if(SDL_BlitSurface(map::tileID[id], NULL, map::mapSurface[gl], &amp;dstRect) != NULL) exit(4); } } } } } } </code></pre> <p>-</p> <pre><code>//source.cpp //---------------------------------------------------------------------------------------------------- #include "../libraries/include.h" //---------------------------------------------------------------------------------------------------- int main(int argc,char *argv[]) { if(SDL_Init(SDL_INIT_EVERYTHING) != NULL) exit(0); SDL_Surface *screen; screen = SDL_SetVideoMode(1024, 768, 32, SDL_HWSURFACE | SDL_DOUBLEBUF); map A; A.getTileID("map.png"); A.getTilePosition("map.txt"); A.drawMap(); screen = A.mapSurface[0]; SDL_Flip(screen); SDL_Delay(1000); SDL_FreeSurface(screen); SDL_Quit(); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload