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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include"LEXSDL.h"
#include<stdio.h>
int main(void){
// init sdl and sdl_image with default flags and check if error ocurres at initialization
if(LEXSDL_Init(0)){
printf("SDL init failed\n");
return -1;
}
if(LEXSDL_InitIMG(0)){
printf("SDLIMG init failed\n");
return -1;
}
// create the window with title, default_flags and check if failed.
if(LEXSDL_CreateWindow("LEXSDL Example - loadingRenderingTextures.c", 0) == NULL){
printf("failed to create SDL_Window\n");
return -1;
}
// create renderer with default flags and check if failed
if(LEXSDL_CreateRenderer(0) == NULL){
printf("failed to create SDL_Renderer\n");
return -1;
}
// load the first texture
// we are loading a file that doesn't exist for demonstration purposes
int pixelDucksID = LEXSDL_TextureLoad("examples/assets/pixelDucksFAIL.png");
// since loading can fail we must check if it failed, negative numbers mean different errors (refer to the documentation), in this case the returned id should be -3 since it was not able to load the texture because the file doesn't exist.
loadPixelDuckCheck: // ignore this, is for avoiding infinite loop in extreme case.
switch(pixelDucksID){
case -1:
printf("could not add the first texture.\nthis should not have happened\n");
return -1;
break;
case -2:
printf("could not add texture.\nthis should not have happened\n");
return -1;
break;
case -3:
// now load the correct file
pixelDucksID = LEXSDL_TextureLoad("examples/assets/pixelDucks.png");
// extra statement to avoid infinite loop in case something is missing/wrong.
// this should not be done in production
static int avoidinfiniteloop = 0;
avoidinfiniteloop++;
if(avoidinfiniteloop > 1){
printf("\033[1;31mcould not load \"examples/assets/pixelDucks.png\".\nthis should not have happened\033[0m\n");
return -1;
}
goto loadPixelDuckCheck;
break;
default:
printf("\"examples/assets/pixelDucks.png\" was loaded\n");
break;
}
// now start a new frame
LEXSDL_NewFrame();
// render the texture
LEXSDL_TextureDrawFill(pixelDucksID);
// show the frame
LEXSDL_ShowFrame();
// sleep for 3 seconds
sleep(3);
/* those are basic for creating a window and loading a texture */
// you can load textures included in the binary after converting them into its binary with tool like xxd
// here is one included with the project
#include"TopPatPng.h"
int TopPatID = LEXSDL_TextureLoadBytes(TopPatPng, TopPatPngLen);
// if you need more complex events it still nesceasary for you to handle them manually.
/* Setup the events */
LEXSDL_SetupEvents();
/* a rendering/game loop */
// it will run until the SDL_QUIT event is triggered and recovered with LEXSDL_EventQuit()
while(!LEXSDL_EventQuit()){
// Update/handle the events
LEXSDL_HandleEvents();
// check for a keypress using scancodes
if(LEXSDL_EventKey(SDL_SCANCODE_ESCAPE))
//set the quit event to true
LEXSDL_EventDoQuit();
// a quit event can be canceled with LEXSDL_EventCancelQuit();
// start a frame.
LEXSDL_NewFrame();
// you can render a texture at a position and size
// don't forget that SDL `y` coordinate is top to bottom!
LEXSDL_TextureDrawAt(TopPatID, 50,50,75,75);
// and textures rotated
LEXSDL_TextureDrawRotAt(pixelDucksID, 50+75,50,300,200,180);
/* you can get the mouse position with different functions
void LEXSDL_MousePos(int *x, int *y);
int LEXSDL_MousePosX(void);
int LEXSDL_MousePosY(void);
also dont forget to update the mouse state!
*/
LEXSDL_MouseUpdate();
int txPos = LEXSDL_MousePosX();
int tyPos = LEXSDL_MousePosY();
static uint8_t trotdir = 0;
static double trot = 0;
/* and get if a click happened */
if(LEXSDL_MouseLeft())
trotdir = 0;
if(LEXSDL_MouseRight())
trotdir = 1;
if(trotdir)
trot++;
else
trot--;
// render a rotating texture at the mouse pos!
LEXSDL_TextureDrawRotAt(TopPatID, txPos,tyPos, 50,50, trot);
// show/render the frame.
LEXSDL_ShowFrame();
}
// and before exiting a cleanup.
// Cleans the internal state and deallocate textures and varius things.
LEXSDL_Terminate();
// Quits SDL and SDL_image.
LEXSDL_Quit();
return 0;
}