#include <iostream>
#include "SDL.h"
#pragma comment(lib, "sdl2.lib")
#pragma comment(lib, "sdl2main.lib")
int main(int argc, char* argv[]) {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL Initialization Fail: %s\n", SDL_GetError());
return -1;
}
SDL_Window* window = SDL_CreateWindow("SDL Test", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_RESIZABLE);
if (!window) {
printf("SDL_CreateWindow Error: %s\n", SDL_GetError());
SDL_Quit();
return -1;
}
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);
SDL_Surface* bmpSurface = SDL_LoadBMP("image.bmp");
// Load a BMP image from a file path.
if (bmpSurface == NULL) {
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
printf("SDL_LoadBMP Error: %s\n", SDL_GetError());
SDL_Quit();
return -1;
}
SDL_Rect destRect = { 0, 0, bmpSurface->w, bmpSurface->h };
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, bmpSurface);
// Create a texture from an existing surface.
if (texture == NULL) {
SDL_FreeSurface(bmpSurface);
// Free an RGB surface.
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
printf("SDL_CreateTextureFromSurface Error: %s\n", SDL_GetError());
SDL_Quit();
return -1;
}
SDL_FreeSurface(bmpSurface);
SDL_Event event;
bool quit = false;
while (!quit) {
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
quit = true;
break;
case SDL_KEYDOWN:
printf("Key pressed: %s\n", SDL_GetKeyName(event.key.keysym.sym));
if (event.key.keysym.sym == SDLK_ESCAPE)
quit = true;
break;
default:
break;
}
}
SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, &destRect);
// Copy a portion of the texture to the current rendering target.
SDL_RenderPresent(renderer);
}
SDL_DestroyTexture(texture);
// Destroy the specified texture.
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}