diff --git a/sdl/whatever/.gitignore b/sdl/whatever/.gitignore new file mode 100644 index 0000000..982793c --- /dev/null +++ b/sdl/whatever/.gitignore @@ -0,0 +1 @@ +whatever diff --git a/sdl/whatever/references b/sdl/whatever/references index 1b29c38..83eeea5 100644 --- a/sdl/whatever/references +++ b/sdl/whatever/references @@ -2,3 +2,4 @@ http://www.xeekworx.com/sdl2guides/11-sdl2guides-02 https://wiki.libsdl.org/CategoryAPI https://wiki.libsdl.org/MigrationGuide http://www.willusher.io/sdl2%20tutorials/2013/08/17/lesson-2-dont-put-everything-in-main +http://lazyfoo.net/tutorials/SDL/18_key_states/index.php diff --git a/sdl/whatever/whatever b/sdl/whatever/whatever deleted file mode 100755 index 9c25478..0000000 Binary files a/sdl/whatever/whatever and /dev/null differ diff --git a/sdl/whatever/whatever.c b/sdl/whatever/whatever.c index 0d44e72..86a75b9 100644 --- a/sdl/whatever/whatever.c +++ b/sdl/whatever/whatever.c @@ -1,7 +1,8 @@ #include "SDL2/SDL.h" - +#include //Write a function that moves an image based on which arrow keys are pressed. //something like +//functionize, everything //void move_image(direction, image); @@ -26,11 +27,12 @@ int main ( int argc, char *argv[] ) SDL_Surface* background_surface; SDL_Texture* background_texture; SDL_Rect top_image_dest = { 0 }; - SDL_Event event = { 0 }; + /*Event handler */ + SDL_Event e; int screen_width = 1024, screen_height = 768; int top_image_width = 0, top_image_height = 0; - + int movement_speed = 1; /* create window of size 640x480 and set title bar*/ @@ -81,13 +83,20 @@ int main ( int argc, char *argv[] ) top_image = SDL_CreateTextureFromSurface(renderer, temp); SDL_FreeSurface(temp); temp = NULL; - /* Get height and width of top_image */ - SDL_QueryTexture(top_image, NULL, NULL, &top_image_width, &top_image_height); if(top_image == NULL) { printf("Failed to create texture from top_image: %s\n", SDL_GetError()); return 1; } + + /* Get height and width of top_image */ + SDL_QueryTexture(top_image, NULL, NULL, &top_image_width, &top_image_height); + + /* Prepare top_image for inital position */ + top_image_dest.x = (screen_width / 2) - (top_image_width / 2); + top_image_dest.y = (screen_width / 2) - (top_image_height / 2); + top_image_dest.w = top_image_width; + top_image_dest.h = top_image_height; } int gameover = 0; @@ -96,38 +105,67 @@ int main ( int argc, char *argv[] ) while (!gameover) { /* look for an event */ - if (SDL_PollEvent(&event)) { +// if (SDL_PollEvent(&event)) { /* an event was found */ - switch (event.type) { +// switch (event.type) { /* close button clicked */ - case SDL_QUIT: - gameover = 1; - break; +// case SDL_QUIT: + // gameover = 1; +// break; + +// switch (event.key.keysym.sym) { +// case SDLK_ESCAPE: +// case SDLK_q: +// gameover = 1; +// break; +// case SDLK_w: +// printf("You just pressed 'w'\n"); +// break; + +// case SDLK_DOWN: +// top_image_dest.y += movement_speed; +// break; */ + /*Try handling the key_events differently */ + + /*handle events on queue */ + while ( SDL_PollEvent( &e ) != 0) + { + if (e.type == SDL_QUIT) + { + gameover = 1; + } + + } + + const Uint8* currentKeyStates = SDL_GetKeyboardState( NULL ); + if (currentKeyStates[ SDL_SCANCODE_ESCAPE ] || + currentKeyStates[ SDL_SCANCODE_Q ] ) + { + gameover = 1; + } + + if (currentKeyStates[ SDL_SCANCODE_LEFT ] ) + { + top_image_dest.x -= movement_speed; + } + if (currentKeyStates[ SDL_SCANCODE_RIGHT ] ) + { + top_image_dest.x += movement_speed; + } + if (currentKeyStates[ SDL_SCANCODE_UP ] ) + { + top_image_dest.y -= movement_speed; + } + if (currentKeyStates[ SDL_SCANCODE_DOWN ] ) + { + top_image_dest.y += movement_speed; + } - /* handle the keyboard */ - case SDL_KEYDOWN: - switch (event.key.keysym.sym) { - case SDLK_ESCAPE: - case SDLK_q: - gameover = 1; - break; - case SDLK_w: - printf("You just pressed 'w'\n"); - break; - } - break; - } - } - /* render the background */ SDL_RenderCopy(renderer, background_texture, NULL, NULL); /* Render entire top_image to center of screen */ - top_image_dest.x = (screen_width / 2) - (top_image_width / 2); - top_image_dest.y = (screen_width / 2) - (top_image_height / 2); - top_image_dest.w = top_image_width; - top_image_dest.h = top_image_height; SDL_RenderCopy(renderer, top_image, NULL, &top_image_dest); /* Render here */