SDL--Why wont the surfaces apply!?!

Hi

So im working on my final project for my intro to C++ class. Im trying to impliment the A* pathfinding algorithm.
The first thing i need to do(and am trying to do) is bang out a basic Node class.
The most important part of this class is that i wont to be able to store all Node objects in an Array(or vector) and i want them to all keep track of their position and display a tile image at their location.

The problem im having right now is that The image only displays once in the top left corner. I need all 192 instances of the object to display the tile image in a 16x12 grid. Since there are 192 total Node objects, so im not sure if the image is only getting displayed once or if its getting displayed 192 times in the same place.
Here is the code for the Node Class, and the function that loops through a list of the objects and calls their apply function to set the object's location and display the image.

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
class Node {
private:
	int x;
	int y;

public:
	//heres the Function that is supposed to apply the image to the screen. 
	void Node::apply(int ex, int why){
		x=ex;
		y=why;
			apply_surface( x, y, tile, screen);}	
};

void Make_Nodes(Node nodes[]){
	int x = 0;
	int y = 0;
	//Node *n = new Node;
	//Node next = n;
	//node array is sized at 192 
	for(int i = 0; i<192; i++){
		nodes[i].apply(x,y);}
	//if last node was placed on the x axis with an x coordinate less than 600, next node will be placed with an coordinate 50 more than current. 
	if(x < 800){
		x += 50;}
	//if last node was placed with a y coordinate less than 600 and the last node was placed as far over on the x axis as its supposed to go(800) than next node will be placed 50 pixels lower
	if(y < 600 && x == 800){
		y += 50;}
	//if last node was placed as far to the rigt as it was supposed to go, next node will be placed as far left as its supposed ot go. 
	if( x == 800){
		x = 0;}
	SDL_Flip( screen );
}

	




Here's the code in its entirety though.

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
#include "SDL.h"
#include <string>
#include <vector>

using namespace std;


//The attributes of the screen
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
const int SCREEN_BPP = 32;

SDL_Surface *message = NULL;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *tile = NULL;

SDL_Surface *load_image( std::string filename ) 
{
    //Temporary storage for the image that's loaded
    SDL_Surface* loadedImage = NULL;
    
    //The optimized image that will be used
    SDL_Surface* optimizedImage = NULL;

	   //Load the image
    loadedImage = SDL_LoadBMP( filename.c_str() );

	  //If nothing went wrong in loading the image
    if( loadedImage != NULL )
    {
        //Create an optimized image
        optimizedImage = SDL_DisplayFormat( loadedImage );
        
        //Free the old image
        SDL_FreeSurface( loadedImage );
    }
	    //Return the optimized image
    return optimizedImage;
}

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
    //Make a temporary rectangle to hold the offsets
    SDL_Rect offset;
    
    //Give the offsets to the rectangle
    offset.x = x;
    offset.y = y;
	    //Blit the surface
    SDL_BlitSurface( source, NULL, destination, &offset );
}
class Node {
private:
	int x;
	int y;

public:
	//heres the Function that is supposed to apply the image to the screen. 
	void Node::apply(int ex, int why){
		x=ex;
		y=why;
			apply_surface( x, y, tile, screen);}	
};

void Make_Nodes(Node nodes[]){
	int x = 0;
	int y = 0;
	//Node *n = new Node;
	//Node next = n;
	//node array is sized at 192 
	for(int i = 0; i<192; i++){
		nodes[i].apply(x,y);}
	//if last node was placed on the x axis with an x coordinate less than 600, next node will be placed with an coordinate 50 more than current. 
	if(x < 800){
		x += 50;}
	//if last node was placed with a y coordinate less than 600 and the last node was placed as far over on the x axis as its supposed to go(800) than next node will be placed 50 pixels lower
	if(y < 600 && x == 800){
		y += 50;}
	//if last node was placed as far to the rigt as it was supposed to go, next node will be placed as far left as its supposed ot go. 
	if( x == 800){
		x = 0;}
	SDL_Flip( screen );
}

	





int main( int argc, char* args[] )
{
	 //Initialize all SDL subsystems
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return 1;    
    }
	 //Set up the screen
    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
	  //If there was an error in setting up the screen
    if( screen == NULL )
    {
        return 1;    
    }
	    //Set the window caption
    SDL_WM_SetCaption( "Hello World", NULL );
	    //Load the images
    message = load_image( "hello.bmp" );
    background = load_image( "background.bmp" );
	tile = load_image("tile.bmp");
	
//	vector<Node> nodes(192);
	Node *nodes = new Node[192];
	Make_Nodes(nodes);

	//Update the screen
    if( SDL_Flip( screen ) == -1 ){
        return 1;    
    }
	 //Wait 2 seconds
    SDL_Delay( 2000 );
    //Free the surfaces
    SDL_FreeSurface( message );
    SDL_FreeSurface( background );
    
    //Quit SDL
    SDL_Quit();
    
    //Return
    return 0;
}
Last edited on
Inside Make_Nodes you call apply with x and y equal to 0 for all the nodes. I suspect you actually meant to put the following if statements inside the loop. Now they are placed outside the loop and doesn't have any impact on anything.

omg wow...thats hilarious. I cant believe i missed that. Thanks a ton though!!
How did the path finding work out?
closed account (yCf9216C)
I'm with gamefanatic, you ought to post a video showing how it worked out! It would be so awesome to see that code in action!
im still working on it. Its due Monday. Im having some minor issues as of right now but ill work through them.
Right now im just trying to create a button that will create an instance of the path class, which essentially will be the object that calculates and displays the path.
However i need to get it so that the button wont create it unless the user has designated a starting node and a goal node. Im having trouble with that last part.
wow nevermind i just fixed it. smh...soo simple. Im so bad at making these simple mistakes and not noticing them. i put an = instead of ==, so the if statement was always true.
Topic archived. No new replies allowed.