148 lines
3.6 KiB
C

#include "SDL2/SDL.h"
//Write a function that moves an image based on which arrow keys are pressed.
//something like
//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 };
SDL_Event event = { 0 };
int screen_width = 1024, screen_height = 768;
int top_image_width = 0, top_image_height = 0;
/* 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;
/* 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;
}
}
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;
/* 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 */
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;
}