SDL projectiles

hey guys i need to make a game that will spawn a new object every 2 seconds
almost like a bullet being shot every 2 seconds and it will move across the screen from left 2 right but i cant seem to get it working... this is my 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 "SDL.h"
#include "SDL_image.h"
#include <vector>
#include <ctime>
#include <string>
#include <iostream>

using namespace std;

//The surfaces
SDL_Surface *image = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *menuScreen = NULL;
SDL_Surface *background = NULL;

//Screen attributes
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
const int SCREEN_BPP = 32;


//The event structure that will be used
SDL_Event event;
 


enum GameState
{
	Menu,
	Level,
	GameOver
}; 

GameState State; 

class WorldObject
{
public:
	int x;
	int y;
	SDL_Surface* surface;

	void Draw()
	{
		apply_surface( x, y, surface, screen);
	}
	virtual void Update(int time)
	{
	}

};


class WorldObjectManager
{
	vector <WorldObject*> WorldList;
	vector <WorldObject*> AddList;
	vector <WorldObject*> RemoveList;

public:
	void AddWorldObject(WorldObject* object)
	{
		WorldList.push_back( object );
	}

	int GetWorldListSize()
	{
		return WorldList.size();
	}

	void DisplayWorld()
	{
		for(int i=0; i < WorldList.size(); i++)
		{
			WorldList[i]->Draw();
		}
	}

	void UpdateWorld(int time)
	{
		MergeAddList();
		for(int i=0; i < WorldList.size(); i++)
		{
			WorldList[i]->Update(time);
		}
	}

	void QueueToAddWorldObject(WorldObject* object)
	{
		AddList.push_back( object );
	}

	void QueueToRemoveWorldObject(WorldObject* object)
	{
		RemoveList.push_back( object );
	}

	void MergeAddList()
	{
		for(int i=0; i < AddList.size(); i++)
		{
			WorldList.push_back(AddList[i]);
		}
		AddList.clear();
	}

	void MergeRemoveList()
	{
		vector<WorldObject*>::iterator iter = RemoveList.begin();
		while( iter != RemoveList.end() )
		{
			WorldList.erase( iter );
			++iter;
		}
		RemoveList.clear();
	}
};
class LivingObject : public WorldObject
{
	int Age;
	int DeathAge;
	int AmountTimeToSpawn;
	int LastSpawnTime;

public:
	LivingObject()
	{
		Age = 0;
		DeathAge = 10000;
		AmountTimeToSpawn = 1500;
		LastSpawnTime = 0;
		surface = load_image("tree1.bmp");
	}
	void Update(int currentTime)
	{
		if( IsDead() )
		{
			cout << "me is dead" << endl;
			manager->QueueToRemoveWorldObject(this);
		}
		
		if( IsReadyToSpawn(currentTime) )
		{
			cout << "spawning, list size: " << manager->GetWorldListSize() << endl;
			//cout << "last spawn time: " << currentTime << endl;
			Spawn();
			LastSpawnTime = currentTime;
		}
		IncreaseAge();
	}
	bool IsDead()
	{
		return Age>=DeathAge;
	}

	bool IsReadyToSpawn(int currentTime)
	{
		int timeElapsed = currentTime - LastSpawnTime;
		return timeElapsed >= AmountTimeToSpawn;
	}

	void IncreaseAge()
	{
		Age++;
	}
	void Spawn()
	{
		LivingObject* object = new LivingObject();
		manager->QueueToAddWorldObject( object );
	}
};


SDL_Surface *load_image( std::string filename )
{
    //The image that's loaded
    SDL_Surface* loadedImage = NULL;

    //The optimized image that will be used
    SDL_Surface* optimizedImage = NULL;

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

    //If the image loaded
    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 )
{
    //Temporary rectangle to hold the offsets
    SDL_Rect offset;

    //Get the offsets
    offset.x = x;
    offset.y = y;

    //Blit the surface
    SDL_BlitSurface( source, NULL, destination, &offset );


}
int main( int argc, char* args[] )
{
    bool quit = false;

    Initalize();

	SetupWorld();

    //While the user hasn't quit
    while( quit == false )
    {
		SDL_Delay(100);
		GameLoop();
		if( SDL_Flip( screen ) == -1 )
		{
			return 1;
		}
        //While there's an event to handle
        while( SDL_PollEvent( &event ) )
        {
			if (event.type == SDL_KEYDOWN)
			{
				if (event.key.keysym.sym == SDLK_RETURN)
					State = Level;
			}
            //If the user has Xed out the window
            if( event.type == SDL_QUIT )
            {
                //Quit the program
                quit = true;
            }
        }
    }

    //Free the surface and quit SDL
    clean_up();

    return 0;


}

void clean_up()
{
    //Free the surface
    SDL_FreeSurface( image );
	SDL_FreeSurface( menuScreen );
    //Quit SDL
    SDL_Quit();
}


please if anybody knows what i'm doing wrong please tell me ... lol
i know its alot to read... but has anyone taken a look or can offer some advice...i might be doing this the hard way...if there's a more efficient route to take i would be more then willing to hear some advice....
Don't forget to delete any memory allocated with operator new().
I am also having the same kind of issue.
closed account (y8h7M4Gy)
why don't you just reset the bullet's x axis after it goes off the other side of the screen?
Do you want to make an open source game? I want a partner for a platform game I'm making :P
closed account (y8h7M4Gy)
also, if you want to make something spawn every certain amount of time.. take a look at this tutorial...

http://lazyfoo.net/SDL_tutorials/lesson28/index.php

it isn't directly telling you how to do it, but it should be easy to figure it out from there

Hope this helps
Topic archived. No new replies allowed.