× Aktuálně z oboru

Programátoři po celém světě dnes slaví Den programátorů [ clanek/2018091300-programatori-po-celem-svete-dnes-slavi-den-programatoru/ ]
Celá zprávička [ clanek/2018091300-programatori-po-celem-svete-dnes-slavi-den-programatoru/ ]

SDL - 8. lekce

[ http://programujte.com/profil/20356-vaclav-milata/ ]Google [ ?rel=author ]       [ http://programujte.com/profil/20356-lucie-z/ ]Google [ ?rel=author ]       8. 12. 2005       18 282×

Jak nastavit systémový timer..

Dnes si napíšeme lepší alternativu sedmé lekce, kdy místo vlastních výpočtů použijeme systémový timer. Navážeme tedy znovu na šestou lekci, avšak opět vynecháme poslední řádek ve funkci main(), inkrementující proměnnou frame.

Pokud používáme systémový timer, musíme jej inicializovat ve funkci SDL_Init() flagem SDL_INIT_TIMER. Jestliže tedy chceme inicializovat grafiku a časovač, bude naše inicializace vypadat takto:


if( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0 ){
  .....
}

Ve funkci main() si vytvoříme proměnnou timer1, typu SDL_TimerID, která bude reprezentovat náš časovač.


SDL_TimerID timer1;

Téměř kdekoliv před hlavní smyčkou ve funkci main() můžeme tento timer aktivovat. To se provádí funkcí SDL_AddTimer(). První parametr této funkce určuje interval timeru v milisekundách, druhý parametr odkazuje na funkci, která se po uplynutí intervalu spustí a třetí této funkci předává nějaký parametr.


timer1 = SDL_AddTimer(300, Timer1, NULL);

Tímto jsme tedy vytvořili timer s intervalem 300ms. Funkci Timer1(), která se po uplynutí intervalu spustí, si za chvíli vytvoříme a třetí parametr momentálně nepotřebujeme, a proto mu předáme hodnotu NULL.

Funkce Timer1() musí mít návratovou hodnotu typu Uint32, která vrací zbylý interval. Má dva parametry, a to interval, který posléze tato funkce vrátí a ukazatel na parametr *param, který můžete uvést při aktivaci timeru ve třetím parametru. Obsahem této funkce bude v našem případě opět inkrementace proměnné frame.


Uint32 Timer1(Uint32 interval, void* param){
  if(frame++ == 3) frame=0;
  return interval;
}

Tímto je timer spuštěn. Pokud byste jej chtěli zastavit, použijte funkci SDL_RemoveTimer():


SDL_RemoveTimer(timer1);

K časovačům je to vše, zde je zdrojový kód lekce:


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

int image_x, image_y;
int frame=0;

Uint32 Timer1(Uint32 interval, void* param){
  if(frame++ == 3) frame=0;
  return interval;
}

void LoadImages(){
  image = SDL_LoadBMP("image.bmp");
  background = SDL_LoadBMP("bg.bmp");
}

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

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


void DrawScene(){
  DrawIMG(background, image_x-1, image_y-1, 130, 130, image_x-1, image_y-1);
  DrawIMG(image, image_x, image_y, 128, 128, frame*128, 0);
  SDL_Flip(screen);
}

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

  if( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0 ){
    printf("Inicializace SDL se nezdařila: %s
", SDL_GetError());
    exit(1);
  }

  atexit(SDL_Quit);
  screen = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);

  if ( screen == NULL ){
    printf("Vytvoření okna se nezdařilo: %s
", SDL_GetError());
    exit(1);
  }
  
  LoadImages();
  DrawIMG(background, 0, 0);
  SDL_SetColorKey(image, SDL_SRCCOLORKEY, SDL_MapRGB(image->format, 0, 0, 0));
  SDL_SetAlpha(image, SDL_SRCALPHA, 128);
  
  timer1 = SDL_AddTimer(300, Timer1, NULL);
  
  bool done=false;

  while(done == false){
    SDL_Event event;
    while(SDL_PollEvent(&event)){
      if(event.type == SDL_QUIT) done = true;
      if(event.type == SDL_KEYUP){
        if(event.key.keysym.sym == SDLK_RETURN) done = true;
      }
    }
    
    keys = SDL_GetKeyState(NULL);
    if(keys[SDLK_RIGHT]) image_x += 1;
    if(keys[SDLK_LEFT]) image_x -= 1;
    if(keys[SDLK_DOWN]) image_y += 1;
    if(keys[SDLK_UP]) image_y -= 1;
    
    DrawScene();
  }

}

Článek stažen z webu Programujte.com [ http://programujte.com/clanek/2005113001-sdl-8-lekce/ ].