tags:

views:

248

answers:

5

Can someone write up a source for a program that just has a "game loop", which just keeps looping until you press Esc, and the program shows a basic image. Heres the source I have right now but I have to use SDL_Delay(2000); to keep the program alive for 2 seconds, during which the program is frozen.

#include "SDL.h"

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

SDL_Surface* hello = NULL;
SDL_Surface* screen = NULL;

SDL_Init(SDL_INIT_EVERYTHING);

screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);

hello = SDL_LoadBMP("hello.bmp");

SDL_BlitSurface(hello, NULL, screen, NULL);

SDL_Flip(screen);

SDL_Delay(2000);

SDL_FreeSurface(hello);

SDL_Quit();

return 0;

}

I just want the program to be open until I press Esc. I know how the loop works, I just don't know if I implement inside the main() function, or outside of it. I've tried both, and both times it failed. If you could help me out that would be great :P

+1  A: 

Check out this link: Simple Engine Framework with SDL specifically the void CEngine::Start() method.

Robb
+3  A: 
#include <conio.h>

...

while (!kbhit())
{
    hello = SDL_LoadBMP("hello.bmp");

    SDL_BlitSurface(hello, NULL, screen, NULL);

    SDL_Flip(screen);
}

...
Inverse
the worst answer among all the others.1)it uses a windows specific library,while SDL is multiplatform;2)SDL already provides its way to handle key events,and mixing that way is baaad;3)all keys terminate the loop, not only Esc (OP forgot his own "specification" in the question and this qualifies him as a not good asker);4)as general way is bad since it does not say how to know which key was hit);5)the loop is "strict" polling for nothing, and flipping for nothing, loading "million" times the image instead of once for ever... cputime eater and memory eater... My first -1
ShinTakezou
Yeah it's not intended as a copy and paste answer, but it is a quick way to get what the OP originally wanted. 3, 4, and 5 are easy to satisfy and expand on.
Inverse
+2  A: 

Since you're already using SDL, you could use the SDL_PollEvent function to run an event loop, checking to see if the key press event was ESC. Looks like this would be along the lines of mySDL_Event.key.keysym.sym == SDLK_ESCAPE.

Marc Bollinger
+2  A: 

Tried with something like

  SDL_Event e;
  while( SDL_WaitEvent(&e) )
  {
    if (e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_ESCAPE) break;
  }

? You can find many tutorials and example out there; just a fast-search example.

Added note: WaitEvent "freezes" the program so you can't do anything .. you just wait; other waiting technics can be desired (as PollEvent, or WaitEvent again after the initializtion of a timer).

ShinTakezou
+2  A: 

Here is a complete and working example. Instead of using a frame-time regulation you can also use SDL_WaitEvent.

#include <SDL/SDL.h>
#include <cstdlib>
#include <iostream>

using namespace std;

const Uint32 fps = 40;
const Uint32 minframetime = 1000 / fps;

int main (int argc, char *argv[])
{

  if (SDL_Init (SDL_INIT_VIDEO) != 0)
  {
    cout << "Error initializing SDL: " << SDL_GetError () << endl;
    return 1;
  }

  atexit (&SDL_Quit);
  SDL_Surface *screen = SDL_SetVideoMode (640, 480, 32, SDL_DOUBLEBUF);

  if (screen == NULL)
  {
    cout << "Error setting video mode: " << SDL_GetError () << endl;
    return 1;
  }

  SDL_Surface *pic = SDL_LoadBMP ("hello.bmp");

  if (pic == NULL)
  {
    cout << "Error loading image: " << SDL_GetError () << endl;
    return 1;
  }

  bool running = true;
  SDL_Event event;
  Uint32 frametime;

  while (running)
  {

    frametime = SDL_GetTicks ();

    while (SDL_PollEvent (&event) != 0)
    {
      switch (event.type)
      {
        case SDL_KEYDOWN: if (event.key.keysym.sym == SDLK_ESCAPE)
                            running = false;
                          break;
      }
    }

    if (SDL_GetTicks () - frametime < minframetime)
      SDL_Delay (minframetime - (SDL_GetTicks () - frametime));

  }

  SDL_BlitSurface (pic, NULL, screen, NULL);
  SDL_Flip (screen);
  SDL_FreeSurface (pic);
  SDL_Delay (2000);

  return 0;

}
gotb