SDL problem (big one)

Started by
1 comment, last by graveyard filla 19 years, 4 months ago
I have a weird problem, I had my application up and running, changed a few things, saw it didn't work, changed back and saw it still didn't work... I really don't see where the error should be. The application compiles fine, but won't display the map anymore, the has to be something wrong with this file:

#include <SDL/SDL_image.h>
#include <SDL/SDL.h>
#include <fstream>
#include <cstring>
#include <sstream>
#include "world.h"
#include "gamedata.h"
#include "log.h"

using namespace std;

#define WORLD_W 40
#define WORLD_H 30


WORLD world[40][30];
     
bool InitWorld()
{
     SDL_Surface *grass = IMG_Load("map.gif");
     SDL_Surface *water = IMG_Load("water.gif");
     SDL_Surface *tree1 = IMG_Load("tree1.gif");
     SDL_Surface *tree2 = IMG_Load("tree2.gif");
     SDL_Surface *warp = IMG_Load("warp.gif");
     
     
     ifstream file_in(gamedata.Map_Name.c_str());
     
     if(!file_in)
     return false;
     
     int x = 0;
     int y = 0;
     
     char tile;
     while(y != WORLD_H)
     {
             file_in >> tile;
             
             switch(tile)
             {
                         case '0':
                              world[x][y].image = grass;
                              world[x][y].value = 0;
                              break;
                         case '1':
                              world[x][y].image = water;
                              world[x][y].value = 1;
                              break;
                         case '2':
                              world[x][y].image = tree1;
                              world[x][y].value = 1;
                              break;
                         case '3':
                              world[x][y].image = tree2;
                              world[x][y].value = 1;
                              break;
                         case '4':
                              world[x][y].image = warp;
                              world[x][y].value = 0;
                              gamedata.wX = x * 20;
                              gamedata.wY = y * 20;
                              break;
                         default:
                                 file_in.close();
                                 return false;
                                 break;
             }
             world[x][y].x = x*20;
             world[x][y].y = y*20;
             x++;
             if(x == WORLD_W)
             {
                  x = 0;
                  y++;
             }
     }
     file_in.close();
     logfile.Write("Loaded " + gamedata.Map_Name + " into the system");
     return true;        
}

void DisplayWorld()
{
     for(int j = 0; j < WORLD_H; j++)
     {
             for(int i = 0; i < WORLD_W; i++)
             {
                     SDL_Rect rect;
                     rect.x = world[j].x;
                     rect.y = world[j].y;
                     SDL_BlitSurface(world[j].image, NULL, gamedata.screen, &rect);
             }
     }
     logfile.Write("Displayed " + gamedata.Map_Name + " onto the screen");
}

string ToString(int number)
{
    stringstream temp;
    temp << number;
    return temp.str();
}  

void ChangeWorld(int x)
{
     string number = ToString(x);
     string world = "worlds/world" + number;
     gamedata.Map_Name = world + ".txt";
     logfile.Write("--------------------------------");
     logfile.Write("Changed worlds to: " + gamedata.Map_Name);
     logfile.Write("--------------------------------");
     InitWorld();
     DisplayWorld();    
}

I think the error should be somewhere in the DisplayWorld() or InitWorld(), but I really can't find it. Maybe it is one of those stupid errors that you just don't see anymore after you have looked over it 100s of times. If the error is not here, I have made the full source code available here. Please give me some pointers as to how I could possibly fixe my problem, Joshua
-----------------------------Sismondi GamesStarted c++ in October 2004...
Advertisement
Do you have a debugger? If so, set a breakpoint after the world is generated, and look in the memory to see if it is generated properly.
i agree with Radup. btw, welcome to the wonderfull world of debugging. the more complicated stuff you try to program, the more annoying, hidden, seamingly random your bugs will appear. better start getting used to it now, because later, when your source is 50 files and 10,000 lines, no one in their right mind will debug it for you. it gets VERY frustrating, i know, and you might spend a week or 2 tracking a single bug (i think this has happend to all of us). this is why im suggesting you get in there and find this for yourself, because you really should start learning how to do this as soon as possible. debugging is just another part of programming, albeit the most annoying part.

good luck.
FTA, my 2D futuristic action MMORPG

This topic is closed to new replies.

Advertisement