Jump to content
  • Advertisement
Sign in to follow this  
DominicHughes

How do I make my game run on its own without pressing a Key?

This topic is 2503 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello how in this code would I make things happen on its own without doing anything without doing a while loop outside the application which would crash the rest of the program?


///////////////////////////////////////////////
//Main Libary
///////////////////////////////////////////////
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
//////////////////////////////////////////////

//Main Entry
int main(int argc, char *argv[])
{
//Done setup
bool done = false;

//Surface For Screen
SDL_Surface *Screen = NULL;

//Screen Setup
Screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);

SDL_Event event;

//While Done = false
while(!done)
{

//While SDL is continous
SDL_PollEvent(&event)

{
case SDL_QUIT:

return 0;

break;
}
}
}

Share this post


Link to post
Share on other sites
Advertisement
The simplest solution is to add whatever code you need to execute after the event switch statement. This will run without any key input.

An alternative is to create a background thread that does the work. This solution requires that you know multi threaded programing

Share this post


Link to post
Share on other sites
Okay I tried what you said and non of it worked this is what I did


///////////////////////////////////////////////
//Main Libary
///////////////////////////////////////////////
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
//////////////////////////////////////////////
//Main Entry
int main(int argc, char *argv[])
{
//Done setup
bool done = false;

//Surface For Screen
SDL_Surface *Screen = NULL;

//Screen Setup
Screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);

SDL_Event event;
int enemyfunction(void *unused)
{
//////////////////////////////////////////////////
//Orange Ghost Knight Logic
/////////////////////////////////////////////////

SDL_Rect OrangeGhostKnightRect;
SDL_Rect StorageRect10;
StorageRect10.x = 400;
StorageRect10.y = 320;

///////////////////////////////////////////////

if(StorageRect10.x == 400)
{
StorageRect10.x += 40;
OrangeGhostKnightRect = StorageRect10;
SDL_BlitSurface(OrangeGhostKnightSurf,NULL,Screen,&OrangeGhostKnightRect);
}

if(StorageRect10.x == 2000)
{
StorageRect10.x -=40;
OrangeGhostKnightRect = StorageRect10;
SDL_BlitSurface(OrangeGhostKnightSurf,NULL,Screen,&OrangeGhostKnightRect);
}
}
//While Done = false
while(!done)
{

//While SDL is continous
SDL_PollEvent(&event)
{
SDL_Thread *EnemyThread;
EnemyThread = SDL_CreateThread(Enemyfunction,NULL);
case SDL_QUIT:
return 0;
break;
}
}
}



And then the orangeghostknight only shows once and then dissappears on the background blitting

Share this post


Link to post
Share on other sites
Oh yea sorry let me redo that



///////////////////////////////////////////////
//Main Libary
///////////////////////////////////////////////
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
//////////////////////////////////////////////
//Main Entry
int main(int argc, char *argv[])
{
//Done setup
bool done = false;

//Surface For Screen
SDL_Surface *Screen = NULL;

//Screen Setup
Screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);

SDL_Event event;
int enemyfunction(void *unused)
{
//////////////////////////////////////////////////
//Orange Ghost Knight Logic
/////////////////////////////////////////////////
SDL_Rect OrangeGhostKnightRect;
SDL_Rect StorageRect10;
StorageRect10.x = 400;
StorageRect10.y = 320;
///////////////////////////////////////////////
if(StorageRect10.x == 400)
{
StorageRect10.x += 40;
OrangeGhostKnightRect = StorageRect10;
SDL_BlitSurface(OrangeGhostKnightSurf,NULL,Screen,&OrangeGhostKnightRect);
}
if(StorageRect10.x == 2000)
{
StorageRect10.x -=40;
OrangeGhostKnightRect = StorageRect10;
SDL_BlitSurface(OrangeGhostKnightSurf,NULL,Screen,&OrangeGhostKnightRect);
}
}
//While Done = false
while(!done)
{

//While SDL is continous
SDL_PollEvent(&event)
{
switch(event.type)
{
SDL_Thread *EnemyThread;
EnemyThread = SDL_CreateThread(Enemyfunction,NULL);
case SDL_QUIT:
return 0;
break;
}
}
}

Share this post


Link to post
Share on other sites
Undergrowth, go back to learning how to program. Start with C++ Tutorial #1 (just pick a good C++ tutorial). When you're finished with all the tutorials, you might could come back to this, and you'll laugh at everything you are doing wrong.

It's obvious you don't know how to program yet. Learn how to program properly 1st, then try some graphics stuff.

Share this post


Link to post
Share on other sites

That code is messed up above the real code is alot better I can ashore you smile.png


Then post the real code so we can help you instead of posting something different that only sidetracks the discussion.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!