whatever: Added 6 degreess of movement
This commit is contained in:
parent
4ec6c8652e
commit
4b7946d046
1
sdl/whatever/.gitignore
vendored
Normal file
1
sdl/whatever/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
whatever
|
@ -2,3 +2,4 @@ http://www.xeekworx.com/sdl2guides/11-sdl2guides-02
|
|||||||
https://wiki.libsdl.org/CategoryAPI
|
https://wiki.libsdl.org/CategoryAPI
|
||||||
https://wiki.libsdl.org/MigrationGuide
|
https://wiki.libsdl.org/MigrationGuide
|
||||||
http://www.willusher.io/sdl2%20tutorials/2013/08/17/lesson-2-dont-put-everything-in-main
|
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
|
||||||
|
Binary file not shown.
@ -1,7 +1,8 @@
|
|||||||
#include "SDL2/SDL.h"
|
#include "SDL2/SDL.h"
|
||||||
|
#include <stdbool.h>
|
||||||
//Write a function that moves an image based on which arrow keys are pressed.
|
//Write a function that moves an image based on which arrow keys are pressed.
|
||||||
//something like
|
//something like
|
||||||
|
//functionize, everything
|
||||||
//void move_image(direction, image);
|
//void move_image(direction, image);
|
||||||
|
|
||||||
|
|
||||||
@ -26,11 +27,12 @@ int main ( int argc, char *argv[] )
|
|||||||
SDL_Surface* background_surface;
|
SDL_Surface* background_surface;
|
||||||
SDL_Texture* background_texture;
|
SDL_Texture* background_texture;
|
||||||
SDL_Rect top_image_dest = { 0 };
|
SDL_Rect top_image_dest = { 0 };
|
||||||
SDL_Event event = { 0 };
|
/*Event handler */
|
||||||
|
SDL_Event e;
|
||||||
|
|
||||||
int screen_width = 1024, screen_height = 768;
|
int screen_width = 1024, screen_height = 768;
|
||||||
int top_image_width = 0, top_image_height = 0;
|
int top_image_width = 0, top_image_height = 0;
|
||||||
|
int movement_speed = 1;
|
||||||
|
|
||||||
|
|
||||||
/* create window of size 640x480 and set title bar*/
|
/* 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);
|
top_image = SDL_CreateTextureFromSurface(renderer, temp);
|
||||||
SDL_FreeSurface(temp);
|
SDL_FreeSurface(temp);
|
||||||
temp = NULL;
|
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) {
|
if(top_image == NULL) {
|
||||||
printf("Failed to create texture from top_image: %s\n", SDL_GetError());
|
printf("Failed to create texture from top_image: %s\n", SDL_GetError());
|
||||||
return 1;
|
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;
|
int gameover = 0;
|
||||||
@ -96,27 +105,60 @@ int main ( int argc, char *argv[] )
|
|||||||
while (!gameover)
|
while (!gameover)
|
||||||
{
|
{
|
||||||
/* look for an event */
|
/* look for an event */
|
||||||
if (SDL_PollEvent(&event)) {
|
// if (SDL_PollEvent(&event)) {
|
||||||
/* an event was found */
|
/* an event was found */
|
||||||
switch (event.type) {
|
// switch (event.type) {
|
||||||
/* close button clicked */
|
/* close button clicked */
|
||||||
case SDL_QUIT:
|
// case SDL_QUIT:
|
||||||
gameover = 1;
|
// gameover = 1;
|
||||||
break;
|
// break;
|
||||||
|
|
||||||
/* handle the keyboard */
|
// switch (event.key.keysym.sym) {
|
||||||
case SDL_KEYDOWN:
|
// case SDLK_ESCAPE:
|
||||||
switch (event.key.keysym.sym) {
|
// case SDLK_q:
|
||||||
case SDLK_ESCAPE:
|
// gameover = 1;
|
||||||
case SDLK_q:
|
// 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;
|
gameover = 1;
|
||||||
break;
|
|
||||||
case SDLK_w:
|
|
||||||
printf("You just pressed 'w'\n");
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -124,10 +166,6 @@ int main ( int argc, char *argv[] )
|
|||||||
SDL_RenderCopy(renderer, background_texture, NULL, NULL);
|
SDL_RenderCopy(renderer, background_texture, NULL, NULL);
|
||||||
|
|
||||||
/* Render entire top_image to center of screen */
|
/* 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);
|
SDL_RenderCopy(renderer, top_image, NULL, &top_image_dest);
|
||||||
|
|
||||||
/* Render here */
|
/* Render here */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user