whatever: functionized tons

This commit is contained in:
Logen Kain 2016-11-12 20:47:34 -07:00
parent 4b7946d046
commit 616383d39b

View File

@ -1,132 +1,69 @@
#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 //functionize, everything
//void move_image(direction, image); //void move_image(direction, image);
/* start SDL and create a window */
int init();
/* load the media durr */
int loadMedia();
/* Free the surfaces and whatnot */
void close_game();
/* Our main window */
SDL_Window* gScreen;
/*screen dimensions*/
const int screen_width = 1024;
const int screen_height = 768;
/* Our renderer */
SDL_Renderer* gRenderer;
/* Surface for loading BMP */
SDL_Surface* gTemp;
/* Texture for background */
SDL_Texture* gBackground_texture;
/* Texture for characters */
SDL_Texture* gPlayer;
/* Player coors */
SDL_Rect gPlayer_dest = { 0 };
int main ( int argc, char *argv[] ) int main ( int argc, char *argv[] )
{ {
/* initialize SDL */ /* How fast our player moves */
if (SDL_Init(SDL_INIT_EVERYTHING) != 0){ int movement_speed = 1;
printf("Unable to init SDL!: %s\n", SDL_GetError());
if (init() == 1)
{
printf("init failed!\n");
close_game();
return 1;
}
if (loadMedia() == 1)
{
printf("loadMedia failed!\n");
close_game();
return 1; return 1;
} }
/* init surfaces */
SDL_Window* screen; //No longer need to do this part I suppose
SDL_Renderer* renderer;
SDL_Surface* temp;
SDL_Texture* top_image;
const char background_imagefile[] = "sdl_logo.bmp";
SDL_Surface* background_surface;
SDL_Texture* background_texture;
SDL_Rect top_image_dest = { 0 };
/*Event handler */ /*Event handler */
SDL_Event e; SDL_Event e;
int screen_width = 1024, screen_height = 768; /* quit if gameover is not 0 */
int top_image_width = 0, top_image_height = 0;
int movement_speed = 1;
/* create window of size 640x480 and set title bar*/
screen = SDL_CreateWindow("SDL TEST",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
screen_width, screen_height,
0);
if (screen == NULL){
printf("Unable to set video mode: %s\n", SDL_GetError());
return 1;
}
renderer = SDL_CreateRenderer(screen, -1, 0);
if (renderer == NULL){
printf("Unable to create renderer: %s\n", SDL_GetError());
}
atexit(SDL_Quit);
/* load bitmap to surface */
background_surface = SDL_LoadBMP(background_imagefile);
if (background_surface == NULL){
printf("Unable to find image!!: %s\n", SDL_GetError());
return 1;
}
else{
/* convert bitmap to texture */
background_texture = SDL_CreateTextureFromSurface(renderer, background_surface);
SDL_FreeSurface(background_surface); // No longer need the surface
background_surface = NULL;
if(background_texture == NULL) {
printf("Failed to create texture from background surface: %s\n", SDL_GetError());
return 1;
}
}
/* load top_image image to practice blit on top of surface*/
temp = SDL_LoadBMP("dinowheel.bmp");
if (temp == NULL){
printf("Unable to load top_image: %s/n", SDL_GetError());
return 1;
}
else{
top_image = SDL_CreateTextureFromSurface(renderer, temp);
SDL_FreeSurface(temp);
temp = NULL;
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; int gameover = 0;
/* message pump */ /* message pump */
while (!gameover) while (!gameover)
{ {
/* look for an event */
// if (SDL_PollEvent(&event)) {
/* an event was found */
// switch (event.type) {
/* close button clicked */
// 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 */ /*handle events on queue */
while ( SDL_PollEvent( &e ) != 0) while ( SDL_PollEvent( &e ) != 0)
{ {
@ -146,40 +83,127 @@ int main ( int argc, char *argv[] )
if (currentKeyStates[ SDL_SCANCODE_LEFT ] ) if (currentKeyStates[ SDL_SCANCODE_LEFT ] )
{ {
top_image_dest.x -= movement_speed; gPlayer_dest.x -= movement_speed;
} }
if (currentKeyStates[ SDL_SCANCODE_RIGHT ] ) if (currentKeyStates[ SDL_SCANCODE_RIGHT ] )
{ {
top_image_dest.x += movement_speed; gPlayer_dest.x += movement_speed;
} }
if (currentKeyStates[ SDL_SCANCODE_UP ] ) if (currentKeyStates[ SDL_SCANCODE_UP ] )
{ {
top_image_dest.y -= movement_speed; gPlayer_dest.y -= movement_speed;
} }
if (currentKeyStates[ SDL_SCANCODE_DOWN ] ) if (currentKeyStates[ SDL_SCANCODE_DOWN ] )
{ {
top_image_dest.y += movement_speed; gPlayer_dest.y += movement_speed;
} }
/* render the background */ /* render the background */
SDL_RenderCopy(renderer, background_texture, NULL, NULL); SDL_RenderCopy(gRenderer, gBackground_texture, NULL, NULL);
/* Render entire top_image to center of screen */ /* Render entire gPlayer to center of screen */
SDL_RenderCopy(renderer, top_image, NULL, &top_image_dest); SDL_RenderCopy(gRenderer, gPlayer, NULL, &gPlayer_dest);
/* Render here */ /* Render here */
SDL_RenderPresent(renderer); SDL_RenderPresent(gRenderer);
} }
/* cleanup SDL */
if (renderer) SDL_DestroyRenderer(renderer);
if (screen) SDL_DestroyWindow(screen);
if (background_texture) SDL_DestroyTexture(background_texture);
if (top_image) SDL_DestroyTexture(top_image);
SDL_Quit();
return 0; return 0;
} }
int init()
{
/* initialize SDL */
if (SDL_Init(SDL_INIT_EVERYTHING) != 0){
printf("Unable to init SDL!: %s\n", SDL_GetError());
return 1;
}
/* 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;
}
return 0;
}
int loadMedia()
{
const char background_imagefile[] = "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("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 */
if (gRenderer) SDL_DestroyRenderer(gRenderer);
if (gScreen) SDL_DestroyWindow(gScreen);
if (gBackground_texture) SDL_DestroyTexture(gBackground_texture);
if (gPlayer) SDL_DestroyTexture(gPlayer);
SDL_Quit();
}