-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
cmake_minimum_required(VERSION 3.17) | ||
project(L03LoadPhoto C) | ||
|
||
set(CMAKE_C_STANDARD 11) | ||
|
||
add_compile_definitions(THREAD_SAFE) | ||
|
||
include_directories( | ||
/usr/local/include/SDL2 | ||
) | ||
|
||
link_directories( | ||
/usr/local/lib | ||
) | ||
|
||
link_libraries( | ||
SDL2 | ||
SDL2_image | ||
) | ||
|
||
add_executable(L03LoadPhoto main.c) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#include <SDL.h> | ||
#include <SDL_image.h> | ||
|
||
#define WIDTH 550 | ||
#define HEIGHT 400 | ||
#define CAT_PHOTO_FILE "cat.jpg" | ||
|
||
SDL_Texture *photo; | ||
|
||
void drawBackground(SDL_Renderer *renderer) { | ||
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); | ||
SDL_Rect rect = {0, 0, WIDTH, HEIGHT}; | ||
SDL_RenderFillRect(renderer, &rect); | ||
} | ||
|
||
void drawScreen(struct SDL_Renderer *renderer) { | ||
if (photo != NULL) { | ||
SDL_Rect dr = {0, 0, 300, 300}; | ||
SDL_RenderCopy(renderer, photo, NULL, &dr); | ||
} | ||
} | ||
|
||
void draw(SDL_Renderer *renderer) { | ||
drawBackground(renderer); | ||
drawScreen(renderer); | ||
} | ||
|
||
int main() { | ||
SDL_Window *win; | ||
SDL_Renderer *renderer; | ||
if (SDL_Init(SDL_INIT_VIDEO)) { | ||
SDL_Log("Unable to init SLD: %s", SDL_GetError()); | ||
return 1; | ||
} | ||
win = SDL_CreateWindow( | ||
"Hello World", | ||
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, | ||
WIDTH, HEIGHT, | ||
SDL_WINDOW_OPENGL | ||
); | ||
if (win == NULL) { | ||
SDL_Log("Unable to create window: %s", SDL_GetError()); | ||
return 1; | ||
} | ||
|
||
renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED); | ||
if (renderer == NULL) { | ||
SDL_Log("Can not create renderer: %s", SDL_GetError()); | ||
return 1; | ||
} | ||
|
||
photo = IMG_LoadTexture(renderer, CAT_PHOTO_FILE); | ||
if (photo == NULL) { | ||
SDL_Log("Can not load %s", CAT_PHOTO_FILE); | ||
} | ||
|
||
while (1) { | ||
SDL_RenderClear(renderer); | ||
draw(renderer); | ||
SDL_RenderPresent(renderer); | ||
|
||
SDL_Event event; | ||
if (SDL_PollEvent(&event)) { | ||
if (event.type == SDL_QUIT) { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
if (photo != NULL) { | ||
SDL_DestroyTexture(photo); | ||
} | ||
SDL_DestroyRenderer(renderer); | ||
SDL_DestroyWindow(win); | ||
SDL_Quit(); | ||
return 0; | ||
} |