Jump to content

SDL Keyboard input


edub13
 Share

Recommended Posts

Boas pessoal, estou a voltar a tentar fazer algumas coisas em SDL, e não estou a conseguir fazer com que a imagem se mova pelo ecrã enquanto carrego numa tecla, so se move clique por clique, como posso resolver isso?

Aqui esta o meu código abaixo


#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <stdio.h>

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

 bool quit = false;

 SDL_Init(SDL_INIT_VIDEO);

 SDL_Event event;

 SDL_Surface* buffer = SDL_SetVideoMode(640, 480, 16, SDL_HWSURFACE);

 if(!buffer){
   printf("%d", SDL_GetError());
return -1;
 }

 SDL_Surface* background = IMG_Load("background.png");

 if(!background){
   printf("%d", SDL_GetError());
return -1;
 }

 SDL_Surface* actor	  = IMG_Load("actor.png");

 if(!actor){
   printf("%d", SDL_GetError());
return -1;
}

 SDL_Rect actor_rect;

 actor_rect.x = 100;
 actor_rect.y = 100;

 while(!quit){

while(SDL_PollEvent(&event)){

  switch(event.type){

case SDL_QUIT:

  quit = true;

  break;

case SDL_KEYDOWN:

  if(event.key.keysym.sym == SDLK_UP)
  {
 actor_rect.y -= 5;
  }

  if(event.key.keysym.sym == SDLK_DOWN)
  {
 actor_rect.y += 5;
  }

  if(event.key.keysym.sym == SDLK_LEFT)
  {
 actor_rect.x -= 5;
  }

  if(event.key.keysym.sym == SDLK_RIGHT)
  {
 actor_rect.x += 5;
  }

  break;

case SDL_KEYUP:

  if(event.key.keysym.sym == SDLK_UP)
  {
 actor_rect.y -= 0;
  }

  if(event.key.keysym.sym == SDLK_DOWN)
  {
 actor_rect.y += 0;
  }

  if(event.key.keysym.sym == SDLK_LEFT)
  {
 actor_rect.x -= 0;
  }

  if(event.key.keysym.sym == SDLK_RIGHT)
  {
 actor_rect.x += 0;
  }

  break;

  }

}

SDL_SetColorKey(actor, SDL_SRCCOLORKEY, SDL_MapRGB(actor->format, 0xFF, 0xFF, 0xFF ));

SDL_BlitSurface(background, NULL, buffer, NULL); // background
SDL_BlitSurface(actor, NULL, buffer, &actor_rect); // actor

SDL_Flip(buffer);

 }

 SDL_FreeSurface(actor);
 SDL_FreeSurface(background);

 SDL_Quit();
 return 0;
}

Cumps

Learning:

C++
Link to comment
Share on other sites

a maneira mais simples de resolver o teu problema será assim :

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <stdio.h>

int main(int argc, char** argv){
 SDL_Init(SDL_INIT_VIDEO);

 SDL_Surface* buffer = SDL_SetVideoMode(640, 480, 16, SDL_HWSURFACE);
 if(!buffer){
printf("%d", SDL_GetError());
SDL_Quit();
return -1;
 }

 SDL_Surface* background = IMG_Load("background.png");
 if(!background){
printf("%d", SDL_GetError());
SDL_Quit();
return -1;
 }

 SDL_Surface* actor = IMG_Load("actor.png");
 if(!actor){
printf("%d", SDL_GetError());
SDL_Quit();
return -1;
 }
 SDL_SetColorKey(actor, SDL_SRCCOLORKEY, SDL_MapRGB(actor->format, 0xFF, 0xFF, 0xFF ));

 SDL_Rect actor_rect;

 actor_rect.x = 100;
 actor_rect.y = 100;

 // -----------------------------------------
 int vel_x = 0;
 int vel_y = 0;
 // -----------------------------------------

 bool quit = false;
 SDL_Event event;
 while(!quit){
while(SDL_PollEvent(&event)){
  switch(event.type){
	case SDL_QUIT:
	  quit = true;
	  break;
 // -----------------------------------------
	case SDL_KEYDOWN:
	  if(event.key.keysym.sym == SDLK_UP)
		vel_y = -5;
	  if(event.key.keysym.sym == SDLK_DOWN)
		vel_y = 5;
	  if(event.key.keysym.sym == SDLK_LEFT)
		vel_x = -5;
	  if(event.key.keysym.sym == SDLK_RIGHT)
		vel_x = 5;
	  break;
	case SDL_KEYUP:
	  if(event.key.keysym.sym == SDLK_UP)
		vel_y = 0;
	  if(event.key.keysym.sym == SDLK_DOWN)
		vel_y = 0;
	  if(event.key.keysym.sym == SDLK_LEFT)
		vel_x = 0;
	  if(event.key.keysym.sym == SDLK_RIGHT)
		vel_x = 0;
	  break;
 // -----------------------------------------
  }
}

 // -----------------------------------------
actor_rect.x += vel_x;
actor_rect.y += vel_y;
 // -----------------------------------------

SDL_BlitSurface(background, NULL, buffer, NULL); // background
SDL_BlitSurface(actor, NULL, buffer, &actor_rect); // actor

SDL_Flip(buffer);
 }

 SDL_FreeSurface(actor);
 SDL_FreeSurface(background);

 SDL_Quit();
 return 0;
}

ps : deverias dar uma olhada no SDL2 ...

pps : esta solução ten spequenos problemas que deverás notar quando testares

Edited by HappyHippyHippo
IRC : sim, é algo que ainda existe >> #p@p
Link to comment
Share on other sites

não notaste nos problemas desta alteração ?

eu dou-te um exemplo:

- carrega na tecla "direita"

- carrega na tecla "esquerda"

- larga a tecla "esquerda"

que teclas estás a carregar e para onde está a ir o "ator" ?

o SDL2 trás várias funcionalidades adicionais, entre elas (e são bastantes) um sistema de touch que facilita a portabilidade para android e ios

IRC : sim, é algo que ainda existe >> #p@p
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.