More SDL and KDevlop

At last I figure out how to use SDL in KDevlop 2.1. My first project for
linux - It Sucks…

When i run my program It seems as it does a few loops and then it wait for
something… and then a few more loops and another wait… and so on… Is
it KDevlop or Linux? I have done this earlier for M$ Windows with Dev-cpp
and had no problem.

/***************************************************************************
main.cpp - description-------------------
begin : s?n sep 29 20:28:11 CEST 2002
copyright : © 2002 by Fredrik Persson
email : perrscout at linux.nu

***************************************************************************/

/***************************************************************************

  •                                                                     *
    
  • This program is free software; you can redistribute it and/or modify *
  • it under the terms of the GNU General Public License as published by *
  • the Free Software Foundation; either version 2 of the License, or *
  • (at your option) any later version. *
  •                                                                     *
    

***************************************************************************/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

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

SDL_Surface *back;
SDL_Surface *screen;
SDL_Surface *image;

int InitImages();
void DrawIMG(SDL_Surface *img, int x, int y);
void DrawIMG(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2);
void DrawBG();
void DrawScene();

int xpos=0;
int ypos=0;

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

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

cout << “perras sdl_pryl f?r linux!” << endl;

if( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) <0 )
{
printf(“Unable to init SDL: %s\n”, SDL_GetError());
return 1;
}

atexit(SDL_Quit);

screen=SDL_SetVideoMode(800,600,16,SDL_FULLSCREEN|SDL_SWSURFACE|SDL_ANYFORMA
T);
//screen=SDL_SetVideoMode(800,600,32,SDL_FULLSCREEN|SDL_HWSURFACE|SDL_DOUBLE
BUF);
if ( screen == NULL )
{
printf(“Unable to set 640x480 video: %s\n”, SDL_GetError());
exit(1);
}

InitImages();
DrawBG();

int done=0;

while(done == 0)
{
SDL_Event event;

while ( SDL_PollEvent(&event) )
{
if ( event.type == SDL_QUIT ) { done = 1; }

if ( event.type == SDL_KEYDOWN )
{
if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
}
}

keys = SDL_GetKeyState(NULL);
if ( keys[SDLK_UP] ) { ypos -= 1; }
if ( keys[SDLK_DOWN] ) { ypos += 1; }
if ( keys[SDLK_LEFT] ) { xpos -= 1; }
if ( keys[SDLK_RIGHT] ) { xpos += 1; }
/*
ypos += 1;

if(ypos > 480){ypos=0;}
*/

DrawScene();
}

return 0;
}

////////////////////////////////////////////////////////////////////////////
/////////////////////////
//misc functions
//

int InitImages()
{
back = SDL_LoadBMP(“bg.bmp”);
image = SDL_LoadBMP(“image.bmp”);

//lite snygg error handle
//if(back==NULL){printf(“hittade inte bg.bmp\n”);}
//if(image==Null){printf(“hittade inte image.bmp”);}
return 0;
}

void DrawIMG(SDL_Surface *img, int x, int y)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_BlitSurface(img, NULL, screen, &dest);
}

void DrawIMG(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_Rect dest2;
dest2.x = x2;
dest2.y = y2;
dest2.w = w;
dest2.h = h;
SDL_BlitSurface(img, &dest2, screen, &dest);
}

void DrawBG()
{
DrawIMG(back, 0, 0);
}

void DrawScene()
{
DrawIMG(back, xpos-2, ypos-2,36, 36, xpos-2, ypos-2);
DrawIMG(image, xpos, ypos);

SDL_Flip(screen);
}