whatever added include;src;whatever.h;updated makefile
This commit is contained in:
274
sdl/whatever/src/whatever.c
Normal file
274
sdl/whatever/src/whatever.c
Normal file
@@ -0,0 +1,274 @@
|
||||
#include "SDL2/SDL.h"
|
||||
|
||||
#include "whatever.h"
|
||||
|
||||
//Write a function that moves an image based on which arrow keys are pressed.
|
||||
//something like
|
||||
//functionize, everything
|
||||
//void move_image(direction, image);
|
||||
|
||||
int main ( int argc, char *argv[] )
|
||||
{
|
||||
/* How fast our player moves */
|
||||
int movement_speed = 1;
|
||||
|
||||
if (init() == 1)
|
||||
{
|
||||
printf("init failed!\n");
|
||||
close_game();
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (loadMedia() == 1)
|
||||
{
|
||||
printf("loadMedia failed!\n");
|
||||
close_game();
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/*Event handler */
|
||||
SDL_Event e;
|
||||
|
||||
/* quit if gameover is not 0 */
|
||||
int gameover = 0;
|
||||
|
||||
/* Normalize directions */
|
||||
int xDir = 0;
|
||||
int yDir = 0;
|
||||
|
||||
/* message pump */
|
||||
while (!gameover)
|
||||
{
|
||||
/*handle events on queue */
|
||||
while ( SDL_PollEvent( &e ) != 0)
|
||||
{
|
||||
if (e.type == SDL_QUIT)
|
||||
{
|
||||
gameover = 1;
|
||||
}
|
||||
|
||||
/* Handle joy button press */
|
||||
else if (e.type == SDL_JOYBUTTONDOWN)
|
||||
{
|
||||
|
||||
/* 0 is bottom right? At least that's what it is on my logitec */
|
||||
/* Changed to number 7 as aparently that's start */
|
||||
if (e.jbutton.button == 7)
|
||||
{
|
||||
gameover = 1;
|
||||
}
|
||||
}
|
||||
|
||||
else if (e.type == SDL_JOYAXISMOTION)
|
||||
{
|
||||
/* Motion on controller 0 */
|
||||
if( e.jaxis.axis == 0 )
|
||||
{
|
||||
/* Left of dead zone */
|
||||
if( e.jaxis.value < -JOYSTICK_DEAD_ZONE )
|
||||
{
|
||||
xDir = -1;
|
||||
}
|
||||
/* Right of dead zone */
|
||||
else if( e.jaxis.value > JOYSTICK_DEAD_ZONE )
|
||||
{
|
||||
xDir = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
xDir = 0;
|
||||
}
|
||||
|
||||
}
|
||||
/* Y axis motion -- Note that this is 1 */
|
||||
if (e.jaxis.axis == 1 )
|
||||
{
|
||||
/*Below the dead zone */
|
||||
|
||||
if(e.jaxis.value < -JOYSTICK_DEAD_ZONE)
|
||||
{
|
||||
yDir = -1;
|
||||
}
|
||||
else if (e.jaxis.value > JOYSTICK_DEAD_ZONE)
|
||||
{
|
||||
yDir = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
yDir = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* move the player with the above input */
|
||||
if (xDir != 0 || yDir != 0)
|
||||
{
|
||||
gPlayer_dest.x += (xDir * movement_speed);
|
||||
gPlayer_dest.y += (yDir * movement_speed);
|
||||
}
|
||||
else
|
||||
{
|
||||
const Uint8* currentKeyStates = SDL_GetKeyboardState( NULL );
|
||||
if (currentKeyStates[ SDL_SCANCODE_ESCAPE ] ||
|
||||
currentKeyStates[ SDL_SCANCODE_Q ] )
|
||||
{
|
||||
gameover = 1;
|
||||
}
|
||||
|
||||
if (currentKeyStates[ SDL_SCANCODE_LEFT ] )
|
||||
{
|
||||
gPlayer_dest.x -= movement_speed;
|
||||
}
|
||||
if (currentKeyStates[ SDL_SCANCODE_RIGHT ] )
|
||||
{
|
||||
gPlayer_dest.x += movement_speed;
|
||||
}
|
||||
if (currentKeyStates[ SDL_SCANCODE_UP ] )
|
||||
{
|
||||
gPlayer_dest.y -= movement_speed;
|
||||
}
|
||||
if (currentKeyStates[ SDL_SCANCODE_DOWN ] )
|
||||
{
|
||||
gPlayer_dest.y += movement_speed;
|
||||
}
|
||||
}
|
||||
|
||||
/* render the background */
|
||||
SDL_RenderCopy(gRenderer, gBackground_texture, NULL, NULL);
|
||||
|
||||
/* Render entire gPlayer to center of screen */
|
||||
SDL_RenderCopy(gRenderer, gPlayer, NULL, &gPlayer_dest);
|
||||
|
||||
/* Render here */
|
||||
SDL_RenderPresent(gRenderer);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int init()
|
||||
{
|
||||
|
||||
/* initialize SDL */
|
||||
if (SDL_Init(SDL_INIT_EVERYTHING) != 0){
|
||||
printf("Unable to init SDL!: %s\n", SDL_GetError());
|
||||
return 1;
|
||||
}
|
||||
/*Hide the mouse cursor */
|
||||
SDL_ShowCursor(0);
|
||||
|
||||
/* create window of size 640x480 and set title bar*/
|
||||
gScreen = SDL_CreateWindow("SDL TEST",
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
screen_width, screen_height,
|
||||
0);
|
||||
if (gScreen == NULL){
|
||||
printf("Unable to set video mode: %s\n", SDL_GetError());
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Check for joysticks */
|
||||
if( SDL_NumJoysticks() < 1 )
|
||||
{
|
||||
printf( "Warning: No joysticks detected!\n" );
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Load Joystick */
|
||||
gGameController = SDL_JoystickOpen( 0 );
|
||||
if ( gGameController == NULL )
|
||||
{
|
||||
printf( "Warning: Unable to open game controller! SDL Error: %s\n", SDL_GetError());
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int loadMedia()
|
||||
{
|
||||
const char background_imagefile[] = "assets/sdl_logo.bmp";
|
||||
|
||||
gRenderer = SDL_CreateRenderer(gScreen, -1, 0);
|
||||
|
||||
if (gRenderer == NULL){
|
||||
printf("Unable to create renderer: %s\n", SDL_GetError());
|
||||
}
|
||||
/* load background bitmap to surface */
|
||||
gTemp = SDL_LoadBMP(background_imagefile);
|
||||
|
||||
if (gTemp == NULL){
|
||||
printf("Unable to find image!!: %s\n", SDL_GetError());
|
||||
return 1;
|
||||
}
|
||||
else{
|
||||
/* convert bitmap to texture */
|
||||
gBackground_texture = SDL_CreateTextureFromSurface(gRenderer, gTemp);
|
||||
SDL_FreeSurface(gTemp); // No longer need the surface
|
||||
gTemp = NULL;
|
||||
if(gBackground_texture == NULL) {
|
||||
printf("Failed to create texture from background surface: %s\n", SDL_GetError());
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* load gPlayer image to practice blit on top of surface*/
|
||||
gTemp = SDL_LoadBMP("assets/dinowheel.bmp");
|
||||
if (gTemp == NULL){
|
||||
printf("Unable to load gPlayer: %s/n", SDL_GetError());
|
||||
return 1;
|
||||
}
|
||||
else{
|
||||
gPlayer = SDL_CreateTextureFromSurface(gRenderer, gTemp);
|
||||
SDL_FreeSurface(gTemp);
|
||||
gTemp = NULL;
|
||||
|
||||
if(gPlayer == NULL) {
|
||||
printf("Failed to create texture from gPlayer: %s\n", SDL_GetError());
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int player_image_width, player_image_height;
|
||||
|
||||
/* Get height and width of gPlayer */
|
||||
SDL_QueryTexture(gPlayer, NULL, NULL, &player_image_width, &player_image_height);
|
||||
|
||||
/* Prepare gPlayer for inital position */
|
||||
gPlayer_dest.x = (screen_width / 2) - (player_image_width / 2);
|
||||
gPlayer_dest.y = (screen_width / 2) - (player_image_height / 2);
|
||||
gPlayer_dest.w = player_image_width;
|
||||
gPlayer_dest.h = player_image_height;
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
void close_game()
|
||||
{
|
||||
|
||||
/* cleanup SDL */
|
||||
|
||||
/* Destroy textures */
|
||||
if (gBackground_texture) SDL_DestroyTexture(gBackground_texture);
|
||||
if (gPlayer) SDL_DestroyTexture(gPlayer);
|
||||
gBackground_texture = NULL;
|
||||
gPlayer = NULL;
|
||||
|
||||
/* Destroy window */
|
||||
if (gRenderer) SDL_DestroyRenderer(gRenderer);
|
||||
if (gScreen) SDL_DestroyWindow(gScreen);
|
||||
gRenderer = NULL;
|
||||
gScreen = NULL;
|
||||
|
||||
/* Close game controller */
|
||||
if (gGameController) SDL_JoystickClose(gGameController);
|
||||
gGameController = NULL;
|
||||
|
||||
/* quit subsystems */
|
||||
SDL_Quit();
|
||||
}
|
||||
Reference in New Issue
Block a user