I have a problem with SDL. As you see, I have 3 files: include.h, map.h, source.cpp.
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.
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.
Nothing happens. Screen loads, and, as I think it flips with empty surface, but it needs to display mapSurface variable.
Please help.
//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 <string>
#include <fstream>
//----------------------------------------------------------------------------------------------------
#include "map.h"
–
//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 < 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 < map::TILEMAP_WIDTH; c++)
{
for(short int r = 0; r < 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, &srcRect, map::tileID[id], &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 >> map::mapWidth;
data.ignore(256, '=');
data >> 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 < map::mapHeight; c++)
{
for(short int r = 0; r < map::mapWidth; r++)
{
data >> 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 >> objX[l] >> objY[l] >> objWidth[l] >> 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 < map::graphicLayer; gl++)
{
for(short int c = 0; c < map::mapWidth; c++)
{
for(short int r = 0; r < map::mapHeight; r++)
{
for(short int id = 0; id < 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], &dstRect) != NULL)
exit(4);
}
}
}
}
}
}
–
//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;
}
It looks like you are drawing your map to
map::mapSurface[0]but this surface is never drawn to the screen. Blit your temporary surface to screen afterdrawMapor draw directly to the screen.