Help with SDL Map Generator

I'm trying to create a random map generator (or at least, pseudo-random) using the SDL library. Every time I run the program I just get a black screen, but no errors. I was wondering if the problem is in the code (and if so, how to fix it) or if it's in the was I set up the libraries.

Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <string>
#include <cstdlib>
#include <time.h>

SDL_Surface *grassTile = NULL;
SDL_Surface *tallgrassTile = NULL;
SDL_Surface *boulderTile = NULL;
SDL_Surface *treeTile = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *dot = NULL;
SDL_Surface *loading = NULL;

const int mapDimension = 1200;
const int tileDimension = 40;
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
const int DOT_WIDTH = 5;
const int DOT_HEIGHT = 5;

SDL_Event event;

SDL_Rect camera = {0,0,SCREEN_WIDTH, SCREEN_HEIGHT};

SDL_Surface *load_image(std::string filename){
	SDL_Surface* loadedImage = NULL;
	SDL_Surface* optimizedImage = NULL;

	loadedImage = IMG_Load(filename.c_str());
	if(loadedImage != NULL){
		optimizedImage = SDL_DisplayFormat(loadedImage);
		SDL_FreeSurface(loadedImage);
}
return optimizedImage;
}

void apply_surface(int x, int y, SDL_Surface *source, SDL_Surface *destination){
	SDL_Rect offset;

	offset.x = x;
	offset.y = y;

	SDL_BlitSurface(source, NULL, destination, &offset);
}

class Dot{
    private:
    int x, y;
    int xVel, yVel;

    public:
    Dot();
    void handle_input();
    void move();
    void show();
    void set_camera();
};

Dot::Dot(){
    x = 600;
    y = 600;

    xVel = 0;
    yVel = 0;
}

void Dot::handle_input(){
    if(event.type == SDL_KEYDOWN){
        switch(event.key.keysym.sym){
            case SDLK_UP: yVel -= DOT_HEIGHT / 2; break;
            case SDLK_DOWN: yVel += DOT_HEIGHT / 2; break;
            case SDLK_LEFT: xVel -= DOT_WIDTH / 2; break;
            case SDLK_RIGHT: xVel += DOT_WIDTH / 2; break;
        }
    }else if(event.type == SDL_KEYUP){
        switch(event.key.keysym.sym){
            case SDLK_UP: yVel += DOT_HEIGHT / 2; break;
            case SDLK_DOWN: yVel -= DOT_HEIGHT / 2; break;
            case SDLK_LEFT: xVel += DOT_WIDTH / 2; break;
            case SDLK_RIGHT: xVel -= DOT_WIDTH / 2; break;
        }
    }
}

void Dot::move(){
    x += xVel;

    if((x < 0 ) || (x + DOT_WIDTH > mapDimension)){
        x -= xVel;
    }

    y += yVel;
    if((y < 0) || (y + DOT_HEIGHT > mapDimension)){
        y -= yVel;
    }
}

void Dot::set_camera(){
    camera.x = (x + DOT_WIDTH / 2) - SCREEN_WIDTH / 2;
    camera.y = (y + DOT_HEIGHT / 2) - SCREEN_HEIGHT / 2;

    if(camera.x < 0){
        camera.x = 0;
    }
    if(camera.y < 0){
        camera.y = 0;
    }
    if(camera.x > mapDimension - camera.w){
        camera.x = mapDimension - camera.w;
    }
    if(camera.y > mapDimension - camera.h){
        camera.y = mapDimension - camera.h;
    }
}

void Dot::show(){
    apply_surface(x - camera.x, y - camera.y, dot, screen);
}

void cleanup(){
    SDL_FreeSurface(tallgrassTile);
    SDL_FreeSurface(boulderTile);
    SDL_FreeSurface(grassTile);
    SDL_FreeSurface(treeTile);

    SDL_Quit();
}

int main(int argc, char* args[]){
    SDL_Init(SDL_INIT_EVERYTHING);

    screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_SWSURFACE);

    SDL_WM_SetCaption("Map Generator", NULL);

    srand(time(NULL));

    Dot myDot;

    int random;
    int x = 1;
    int y = 1;
    bool quit = false;
    bool mapcont = true;
    bool camerplace = false;

    tallgrassTile = load_image("tallgrass.png");
    boulderTile = load_image("boulder.png");
    grassTile = load_image("grass.png");
    loading = load_image("loading.png");
    treeTile = load_image("tree.png");

    while(quit == false){
    while(mapcont == true){
        apply_surface(camera.x, camera.y, loading, screen);
        random = 1+(rand()%10);
        if((random == 1) || (random == 2) || (random == 3) || (random == 4)){
            apply_surface(x, y, grassTile, screen);
            x + tileDimension;
        }else if((random == 5) || (random == 6) || (random == 7)){
            apply_surface(x, y, treeTile, screen);
            x + tileDimension;
        }else if((random == 8) || (random == 9)){
            apply_surface(x, y, tallgrassTile, screen);
            x + tileDimension;
        }
        if(x >= 400){
            x = 1;
            y + tileDimension;
        }
        if(y >= 400){
            mapcont = false;
            SDL_FreeSurface(loading);
        }
    }
    while(mapcont == false){
        while(SDL_PollEvent(&event)){
            myDot.handle_input();
            if(event.type == SDL_QUIT){
                quit = true;
            }
        }
        myDot.move();
        myDot.set_camera();
        myDot.show();
        SDL_Flip(screen);
    }
    }
}
Topic archived. No new replies allowed.