Images don't display

closed account (NUj6URfi)
My code is:

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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252

#include <iostream>
#include <string>
#include <cstdlib>
#include <windows.h>
#include <fstream>
#include "SDL.h"
#include <map>

using namespace std;

int b, a, roomnum;

void firstroom();
void secondroom();

bool gamestatus;
bool pokemon1yn = false;
bool pokemon2yn = false;
bool pokemon3yn = false;
bool pokemon4yn = false;
bool pokemon5yn = false;
bool pokemon6yn = false;
int x, y;
    const int SCREEN_WIDTH = 10;
    const int SCREEN_HEIGHT = 11;
    int PLAYER_WIDTH = 5;
    int PLAYER_HEIGHT = 5;
    const int MAPTOTAL_HEIGHT = 11;
    const int MAPTOTAL_WIDTH = 10;
    
	SDL_Surface* floor = NULL;
	SDL_Surface* player2 = NULL;
    SDL_Surface* screen = NULL;
    
    int room;
    int xwall, ywall;
string PLAYERTOTAL_WIDTH2,PLAYERTOTAL_HEIGHT2, room3;

int PLAYERTOTAL_WIDTH, PLAYERTOTAL_HEIGHT;
    
    
    SDL_Surface *load_image( std::string filename ){
    SDL_Surface* loadedImage = NULL;
    SDL_Surface* optimizedImage = NULL;
    loadedImage = SDL_LoadBMP( 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 );
}



int main( int argc, char *argv[] ) {
    
	SDL_Init(SDL_INIT_EVERYTHING);
	
	Uint8 *key;
	Uint8 *player;
    
	gamestatus = true;

	ifstream load("xlocation.txt", ios::ate);
	PLAYERTOTAL_WIDTH2 = "xlocation.txt", ios::ate;
	PLAYERTOTAL_WIDTH = atoi(PLAYERTOTAL_WIDTH2.c_str());
    load.close();

	ifstream file1("ylocation.txt", ios::ate);
	PLAYERTOTAL_HEIGHT2 = "ylocation.txt";
	PLAYERTOTAL_HEIGHT = atoi(PLAYERTOTAL_HEIGHT2.c_str());
	file1.close();

	
	SDL_WM_SetCaption("Pokemon", "Pokemon");
	screen = SDL_SetVideoMode( 640, 480, 32, SDL_HWSURFACE );
	
	while (gamestatus = true) {
          
    SDL_Flip( screen );
        PLAYERTOTAL_HEIGHT = PLAYER_HEIGHT;
		PLAYERTOTAL_WIDTH = PLAYER_WIDTH;



	ifstream roomd("room.txt", ios::ate);
	room3 = "room.txt", ios::ate;
	if (room3 == "room1")
	firstroom();
	if (room3 == "room2")
	secondroom();
    roomd.close();
    
        SDL_Surface* loadedImage2 = FALSE;
        player2 = load_image( "player.bmp" );
        apply_surface( x, y, player2, screen );
        SDL_Delay( 2500 );
    
	key = SDL_GetKeyState( FALSE );
	
    if (key[SDLK_UP]) {
	    y++; 
	     if (roomnum == 1) {
         if (y == 11) {
        if (x == 5 or 6) {
    secondroom(); }
    else if (y == 11) {
         if (x != 5 or 6) {
   y--; } }
   else if (y == 1) {
        y--;}
        else if (x == 1 or x == 10) {
             y--; }}
}}
	else if (key[SDLK_DOWN]) {
	    y--; 
        if (roomnum == 1) {
         if (y == 11) {
        if (x == 5 or 6) {
    secondroom(); }
    else if (y == 11) {
         if (x != 5 or 6) {
   y++; } }
   else if (y == 1) {
        y++;}
        else if (x == 1 or x == 10) {
             y++; }}
}}
	else if (key[SDLK_LEFT]) {
	    x--; 
        if (roomnum == 1) {
         if (y == 11) {
        if (x == 5 or 6) {
    secondroom(); }
    else if (y == 11) {
         if (x != 5 or 6) {
   x++; } }
   else if (y == 1) {
        x++;}
        else if (x == 1 or x == 10) {
             x++; }}
}}
	else if (key[SDLK_RIGHT]) {
	    x++; 
        if (roomnum == 1) {
         if (y == 11) {
        if (x == 5 or 6) {
    secondroom(); }
    else if (y == 11) {
         if (x != 5 or 6) {
   x--; } }
   else if (y == 1) {
        x--;}
        else if (x == 1 or x == 10) {
             x--; }}
}}
	else if (key[SDLK_z]) {
	}
	else if (key[SDLK_x]) {
		
	}
	else if (key[SDLK_LSHIFT]) {
		
	}
	
	else if (key[SDLK_s]) {
		ofstream file2( "ylocation.txt", ios::trunc | ios::out );
		ofstream file3( "xlocation.txt", ios::trunc | ios::out );
		ofstream file10( "room.txt", ios::trunc | ios::out );
        x = MAPTOTAL_WIDTH - PLAYERTOTAL_WIDTH;
        y = MAPTOTAL_HEIGHT - PLAYERTOTAL_HEIGHT;
        file3 << x;
        file2 << y;
        file10 << room;
		file2.close();
		file3.close();
		file10.close();
		
		if(pokemon1yn != FALSE){
		ofstream file4( "pokemon1.txt", ios::trunc | ios::out);
		
		file4.close();
	}
		if(pokemon2yn != FALSE) {
		ofstream file5( "pokemon2.txt", ios::trunc | ios::out);
		
		file5.close();
	}
		if(pokemon3yn != FALSE) {
		ofstream file6( "pokemon3.txt", ios::trunc | ios::out);
		
		file6.close();
	}
		if(pokemon4yn != FALSE) {
		ofstream file7( "pokemon4.txt", ios::trunc | ios::out);
		
		file7.close();
	}
		if(pokemon5yn != FALSE) {
		ofstream file8( "pokemon5.txt", ios::trunc | ios::out);
		
		file8.close();
	}
		if(pokemon6yn != FALSE) {
		ofstream file9("pokemon6.txt", ios::trunc | ios::out);
		
		file9.close();
	}
	
		gamestatus = false;
		
	}

		if (room3 == "room1") {
        if (x == 11) {
              if (y == 5 or y == 6) {
                    secondroom();
                    x = 0;
                    }}}
	
}

SDL_FreeSurface( player2 );
    SDL_FreeSurface( floor );
    SDL_Quit();
	system("PAUSE");
	return 0;
}

void firstroom() {
     roomnum = 1;
     floor = load_image( "walkablefloor.bmp" );
    apply_surface( 0, 0, floor, screen );
     SDL_Flip( screen );
     SDL_Delay( 2500 );
return ; } 


void secondroom(){
roomnum = 2;
return;  }



The problem is that the images don't display and the window doesn't respond. Please help. Thanks.
89
90
91
92
93
	while (gamestatus = true) { // assignment

// should be

	while (gamestatus == true) { // comparison 


Perhaps you have more mistakes, but the above is what was obvious to me.

If you have problems with SDL, I would suggest that you post on the SDL forums instead.
We can help you with C++ questions, but remember that most of us never used SDL, and probably never will.

http://forums.libsdl.org/index.php
Last edited on
closed account (NUj6URfi)
Thanks.

Anyone else have any help?
closed account (NUj6URfi)
Anyone?
I think these lines are wrong. Check what the value of each of these variables is. For example, I don't think room3 is equal to "room1" or "room2" so floor image is never loaded.

1
2
3
PLAYERTOTAL_WIDTH2 = "xlocation.txt", ios::ate;
	PLAYERTOTAL_HEIGHT2 = "ylocation.txt";
	room3 = "room.txt", ios::ate;


Also when you call apply_surface( x, y, player2, screen ); you haven't given x or y any values. Maybe the player is drawn off the screen.
closed account (NUj6URfi)
Thanks for catching that.

Updated 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262

#include <iostream>
#include <string>
#include <windows.h>
#include <fstream>
#include "SDL.h"
#include <cstring>
#include <stdio.h>
using namespace std;

int b, a, roomnum;

int firstroom();
void secondroom();

SDL_Surface *load_image( std::string filename );

bool gamestatus;
bool pokemon1yn = false;
bool pokemon2yn = false;
bool pokemon3yn = false;
bool pokemon4yn = false;
bool pokemon5yn = false;
bool pokemon6yn = false;

char input_line[MAX_PATH + 1];

int x, y;
    int SCREEN_WIDTH = 10;
    int SCREEN_HEIGHT = 11;
    int PLAYER_WIDTH = 5;
    int PLAYER_HEIGHT = 5;
    int MAPTOTAL_HEIGHT = 11;
    int MAPTOTAL_WIDTH = 10;
    int hfhf = 1;
    
	SDL_Surface* floor = NULL;
	SDL_Surface* player2 = NULL;
    SDL_Surface* screen = NULL;
    
    int room;
    int xwall, ywall;
string PLAYERTOTAL_WIDTH2,PLAYERTOTAL_HEIGHT2, room3;

int PLAYERTOTAL_WIDTH, PLAYERTOTAL_HEIGHT;
    
    
    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 );
}

int main( int argc, char *argv[] ) {
    cout << "Test";
    hfhf++;
	SDL_Init(SDL_INIT_EVERYTHING);
	
	Uint8 *key;
	Uint8 *player;
    
	gamestatus = true;

	ifstream load("xlocation.txt", ios::ate);
	PLAYERTOTAL_WIDTH2 = "xlocation.txt", ios::ate;
	PLAYERTOTAL_WIDTH = atoi(PLAYERTOTAL_WIDTH2.c_str());
    load.close();

	ifstream file1("ylocation.txt", ios::ate);
	PLAYERTOTAL_HEIGHT2 = "ylocation.txt";
	PLAYERTOTAL_HEIGHT = atoi(PLAYERTOTAL_HEIGHT2.c_str());
	file1.close();

	ifstream roomd("room.txt", ios::ate);       
        roomd.getline(input_line, MAX_PATH);
	room3 = input_line;
	if (room3 == "room1") {
	firstroom(); }
	if (room3 == "room2") {
	secondroom(); }
    roomd.close();
    
	SDL_WM_SetCaption("Pokemon", "Pokemon");
	screen = SDL_SetVideoMode( 640, 480, 32, SDL_HWSURFACE );
	
	while (gamestatus == true) {
          SDL_WM_SetCaption("Pokemon2", "Pokemon2");
    if (hfhf != 1) {
             SDL_Flip( screen ); }
        PLAYERTOTAL_HEIGHT = PLAYER_HEIGHT;
		PLAYERTOTAL_WIDTH = PLAYER_WIDTH;



	
    SDL_WM_SetCaption("Pokemon6", "Pokemon6");
        player2 = load_image( "player.bmp" );
        SDL_WM_SetCaption("Pokemon7", "Pokemon7");
        
    apply_surface(PLAYERTOTAL_WIDTH, PLAYERTOTAL_HEIGHT, player2, screen );
        SDL_WM_SetCaption("Pokemon8", "Pokemon8");
        SDL_Delay( 2500 );
    
	key = SDL_GetKeyState( FALSE );
	
    if (key[SDLK_UP]) {
	    y++; 
	     if (roomnum == 1) {
         if (y == 11) {
        if (x == 5 || x == 6) {
    secondroom(); }
    else if (y == 11) {
         if (x != 5 || x != 6) {
   y--; } }
   else if (y == 1) {
        y--;}
        else if (x == 1 || x == 10) {
             y--; }}
}}
	else if (key[SDLK_DOWN]) {
	    y--; 
        if (roomnum == 1) {
         if (y == 11) {
        if (x == 5 || x == 6) {
    secondroom(); }
    else if (y == 11) {
         if (x != 5 ||  x != 6) {
   y++; } }
   else if (y == 1) {
        y++;}
        else if (x == 1 || x == 10) {
             y++; }}
}}
	else if (key[SDLK_LEFT]) {
	    x--; 
        if (roomnum == 1) {
         if (y == 11) {
        if (x == 5 || x == 6) {
    secondroom(); }
    else if (y == 11) {
         if (x != 5 || x != 6) {
   x++; } }
   else if (y == 1) {
        x++;}
        else if (x == 1 || x == 10) {
             x++; }}
}}
	else if (key[SDLK_RIGHT]) {
	    x++; 
        if (roomnum == 1) {
         if (y == 11) {
        if (x == 5 || x == 6) {
    secondroom(); }
    else if (y == 11) {
         if (x != 5 || x != 6) {
   x--; } }
   else if (y == 1) {
        x--;}
        else if (x == 1 || x == 10) {
             x--; }}
}}
	else if (key[SDLK_z]) {
	}
	else if (key[SDLK_x]) {
		
	}
	else if (key[SDLK_LSHIFT]) {
		
	}
	
	else if (key[SDLK_s]) {
		ofstream file2( "ylocation.txt", ios::trunc | ios::out );
		ofstream file3( "xlocation.txt", ios::trunc | ios::out );
		ofstream file10( "room.txt", ios::trunc | ios::out );
        x = MAPTOTAL_WIDTH - PLAYERTOTAL_WIDTH;
        y = MAPTOTAL_HEIGHT - PLAYERTOTAL_HEIGHT;
        file3 << x;
        file2 << y;
        file10 << room;
		file2.close();
		file3.close();
		file10.close();
		
		if(pokemon1yn != FALSE){
		ofstream file4( "pokemon1.txt", ios::trunc | ios::out);
		
		file4.close();
	}
		if(pokemon2yn != FALSE) {
		ofstream file5( "pokemon2.txt", ios::trunc | ios::out);
		
		file5.close();
	}
		if(pokemon3yn != FALSE) {
		ofstream file6( "pokemon3.txt", ios::trunc | ios::out);
		
		file6.close();
	}
		if(pokemon4yn != FALSE) {
		ofstream file7( "pokemon4.txt", ios::trunc | ios::out);
		
		file7.close();
	}
		if(pokemon5yn != FALSE) {
		ofstream file8( "pokemon5.txt", ios::trunc | ios::out);
		
		file8.close();
	}
		if(pokemon6yn != FALSE) {
		ofstream file9("pokemon6.txt", ios::trunc | ios::out);
		
		file9.close();
	}
	
		gamestatus = false;
		
	}

		if (room3 == "room1") {
        if (x == 11) {
              if (y == 5 || y == 6) {
                    secondroom();
                    x = 0;
                    }}}
	
}

SDL_FreeSurface( player2 );
    SDL_FreeSurface( floor );
    SDL_Quit();
	system("PAUSE");
	return 0;
}

int firstroom() {
     SDL_WM_SetCaption("Pokemon3", "Pokemon3");
     roomnum = 1;
     floor = load_image( "walkablefloor.bmp" );
    apply_surface( 250, 275, floor, screen );
     SDL_Flip( screen );
     SDL_Delay( 2500 );
     SDL_WM_SetCaption("Pokemon4", "Pokemon4");
     return 0; } 


void secondroom(){
roomnum = 2;
return;  }

SDL_Surface *load_image( std::string filename ){
    SDL_Surface* loadedImage = NULL;
    SDL_Surface* optimizedImage = NULL;
    loadedImage = SDL_LoadBMP( filename.c_str() );
    if( loadedImage != NULL ){
        optimizedImage = SDL_DisplayFormat( loadedImage );
        SDL_FreeSurface( loadedImage );
    }
    return optimizedImage;
}

Topic archived. No new replies allowed.