186 lines
4.5 KiB
C
186 lines
4.5 KiB
C
#include "SDL2/SDL.h"
|
|
#include <stdbool.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[] )
|
|
{
|
|
/* initialize SDL */
|
|
if (SDL_Init(SDL_INIT_EVERYTHING) != 0){
|
|
printf("Unable to init SDL!: %s\n", SDL_GetError());
|
|
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 */
|
|
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*/
|
|
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;
|
|
|
|
/* message pump */
|
|
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 */
|
|
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;
|
|
}
|
|
|
|
|
|
/* render the background */
|
|
SDL_RenderCopy(renderer, background_texture, NULL, NULL);
|
|
|
|
/* Render entire top_image to center of screen */
|
|
SDL_RenderCopy(renderer, top_image, NULL, &top_image_dest);
|
|
|
|
/* Render here */
|
|
SDL_RenderPresent(renderer);
|
|
}
|
|
|
|
/* 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;
|
|
|
|
}
|