Skip to content

Instantly share code, notes, and snippets.

@wldomiciano
Last active April 16, 2020 00:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wldomiciano/533f83bd399409ef723beff401fa22f2 to your computer and use it in GitHub Desktop.
Save wldomiciano/533f83bd399409ef723beff401fa22f2 to your computer and use it in GitHub Desktop.
Demonstração das funções para lidar com eventos de teclado usando SDL 2 com um protótipo simples, os exemplos estão descritos nesta postagem: https://wldomiciano.com/sdl-2-como-lidar-com-eventos-unicos-do-teclado/
// Testado no MinGW64
// gcc -Wall -Wextra -Wpedantic -Wno-unused-parameter game.c `sdl2-config --cflags --libs`
#include <SDL.h>
SDL_Window* window;
SDL_Renderer* renderer;
SDL_bool quit = SDL_FALSE;
// A tecla está pressionada?
SDL_bool isKeyPressed(int key) {
return SDL_GetKeyboardState(NULL) [key];
}
// Variável global
Uint8 keyStates[SDL_NUM_SCANCODES];
// Reinicia estados
void resetKeyStates() {
for (size_t i = 0; i < SDL_NUM_SCANCODES; i++)
keyStates[i] = 0;
}
// A tecla foi pressinada?
Uint8 wasKeyPressed(int key) {
return keyStates[key] == 1;
}
// A tecla foi liberada?
Uint8 wasKeyReleased(int key) {
return keyStates[key] == 2;
}
void handleInput() {
resetKeyStates();
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
quit = SDL_TRUE;
break;
case SDL_KEYDOWN:
keyStates[event.key.keysym.scancode] = !event.key.repeat;
break;
case SDL_KEYUP:
keyStates[event.key.keysym.scancode] = 2;
break;
}
}
}
void createGame(int w, int h) {
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow("Window Title", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, 0);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC);
}
void destroyGame() {
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
void draw(SDL_Rect* rect, Uint32 color) {
Uint8 r = (color >> 16) & 0xFF;
Uint8 g = (color >> 8) & 0xFF;
Uint8 b = (color) & 0xFF;
SDL_SetRenderDrawColor(renderer, r, g, b, SDL_ALPHA_OPAQUE);
SDL_RenderFillRect(renderer, rect);
}
void collide(SDL_Rect* a, SDL_Rect* b) {
if ( SDL_HasIntersection(a, b) ) {
SDL_Rect c;
SDL_IntersectRect(a, b, &c);
a->y -= c.h;
}
}
SDL_Rect player = { 10, 180, 30, 30};
SDL_Rect ground = { 0, 210, 360, 30};
const int VELOCITY = 5;
const int GRAVITY = 5;
const int JUMP_FORCE = 80;
void update() {
player.y += GRAVITY;
if ( wasKeyPressed(SDL_SCANCODE_SPACE) )
player.y -= JUMP_FORCE;
if ( isKeyPressed(SDL_SCANCODE_RIGHT) )
player.x += VELOCITY;
else if ( isKeyPressed(SDL_SCANCODE_LEFT) )
player.x -= VELOCITY;
collide(&player, &ground);
draw(&player, 0xFF0000);
draw(&ground, 0x00FF00);
}
int main(int argc, char *argv[]) {
createGame(360, 240);
while ( !quit ) {
handleInput();
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0xFF);
SDL_RenderClear(renderer);
update();
SDL_RenderPresent(renderer);
}
destroyGame();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment