-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello_sdl.c
125 lines (107 loc) · 3.75 KB
/
hello_sdl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_log.h>
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_ttf.h>
#define WINDOW_TITLE "SDL Example"
#define EXAMPLE_TEXT "Example text"
#define EXAMPLE_TEXT_SIZE 30
#define EXAMPLE_TEXT_COLOR (SDL_Color) { 255, 0, 0, 255 }
#define FONT_PATH "lemon.ttf"
#define MUSIC_PATH "Secret Melody.mp3"
#define MUSIC_LOOPS -1 // infinite loop
#define AUDIO_CHANNEL_COUNT 16
#define IMAGE_PATH "bg.jpg"
#define SDL_ERROR(asst, str) \
(!(asst) \
? SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, str ": %s", SDL_GetError()), \
exit(1) : 0)
int main(int argc, char **argv) {
// Initialize SDL
SDL_Init(SDL_INIT_EVERYTHING);
IMG_Init(IMG_INIT_PNG | IMG_INIT_JPG);
Mix_Init(MIX_INIT_MP3);
TTF_Init();
puts("SDL initialized");
// Create window
SDL_Window *window = SDL_CreateWindow(
WINDOW_TITLE, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600,
SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
SDL_ERROR(window, "Failed to create window");
SDL_Renderer *renderer =
SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_ERROR(renderer, "Failed to create renderer");
puts("Window created");
// Load image
SDL_Surface *imageSurface = IMG_Load(IMAGE_PATH);
SDL_ERROR(imageSurface, "Failed to load image");
SDL_Texture *imageTexture =
SDL_CreateTextureFromSurface(renderer, imageSurface);
SDL_ERROR(imageTexture, "Failed to create texture");
SDL_FreeSurface(imageSurface);
puts("Image loaded");
// Load font
TTF_Font *example_text_font = TTF_OpenFont(FONT_PATH, EXAMPLE_TEXT_SIZE);
SDL_ERROR(example_text_font, "Failed to load font");
puts("Font loaded");
// Load music
int audio_failed = Mix_OpenAudio(44100, AUDIO_S32, 2, 4096);
SDL_ERROR(!audio_failed, "Failed to open audio channel");
int channels = Mix_AllocateChannels(AUDIO_CHANNEL_COUNT);
SDL_ERROR(channels == AUDIO_CHANNEL_COUNT, "Failed to allocate channels");
Mix_Music *music = Mix_LoadMUS(MUSIC_PATH);
SDL_ERROR(music, "Failed to load music");
puts("Music loaded");
// Play music
Mix_VolumeMusic(MIX_MAX_VOLUME / 2);
audio_failed = Mix_PlayMusic(music, MUSIC_LOOPS);
SDL_ERROR(!audio_failed, "Failed to play music");
puts("Music playing");
// Render text
SDL_Color textColor = EXAMPLE_TEXT_COLOR;
SDL_Surface *textSurface =
TTF_RenderText_Solid(example_text_font, EXAMPLE_TEXT, textColor);
SDL_ERROR(textSurface, "Failed to render text");
SDL_Texture *textTexture =
SDL_CreateTextureFromSurface(renderer, textSurface);
SDL_ERROR(textTexture, "Failed to create texture");
SDL_Rect textRect = {50, 50, textSurface->w, textSurface->h};
SDL_FreeSurface(textSurface);
puts("Text rendered");
// Main loop
SDL_Event e;
int screen_dirty = 1;
int quit = 0;
while (1) {
while (SDL_PollEvent(&e)) {
printf("Event: %" PRIu32 "\n", e.type);
if (e.type == SDL_QUIT)
goto exit_main_loop;
else if (e.type == SDL_WINDOWEVENT)
if (e.window.event != SDL_WINDOWEVENT_MOVED)
screen_dirty = 1;
if (screen_dirty) {
screen_dirty = 0;
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, imageTexture, NULL, NULL);
SDL_RenderCopy(renderer, textTexture, NULL, &textRect);
SDL_RenderPresent(renderer);
puts("Screen rendered");
}
}
}
// Cleanup
exit_main_loop:
Mix_CloseAudio();
Mix_FreeMusic(music);
SDL_DestroyTexture(textTexture);
SDL_DestroyTexture(imageTexture);
TTF_CloseFont(example_text_font);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
TTF_Quit();
IMG_Quit();
SDL_Quit();
puts("SDL cleaned up");
return 0;
}