Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7404967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:19:58+00:00 2026-05-29T05:19:58+00:00

I have a problem with SDL. As you see, I have 3 files: include.h,

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:19:59+00:00Added an answer on May 29, 2026 at 5:19 am

    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 after drawMap or draw directly to the screen.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have converted C++ source from using GLUT to using SDL/OpenGL The problem is
I have a problem with a timer class based on a SDL timer. class
This is an SDL problem, however I have the strong feeling that the problem
I have problem with return statment >.< I want to store all magazine names
i have problem with autorotate on iphone i set up in all classes -
I have a C-code which I have not managed to run http://dl.getdropbox.com/u/175564/problem-sdl.png The problem
I use SDL for my programs graphics and I have a problem with it
I really have no problem with SDL in C , but it's kind of
I have a problem with different sound libraries... I use Visual Studio.NET and C#
I have problem in some JavaScript that I am writing where the Switch statement

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.