pico-sdl
Loading...
Searching...
No Matches
pico.h
1#ifndef PICO_H
2#define PICO_H
3
4#ifdef __cplusplus
5extern "C" {
6#endif
7
8#include <stdio.h>
9#include <assert.h>
10#include <SDL2/SDL.h>
11#include "keys.h"
12#include "events.h"
13
14/// @example init.c
15/// @example delay.c
16/// @example event.c
17/// @example event_timeout.c
18/// @example event_loop.c
19
20/// @defgroup Types
21/// @brief Types, Enums, and Defines.
22/// @{
23///
24#define PICO_TITLE "pico-SDL"
25#define PICO_DIM_PHY ((Pico_Dim) {640,360})
26#define PICO_DIM_LOG ((Pico_Dim) { 64, 36})
27#define PICO_HASH 128
28
29typedef SDL_Point Pico_Pos;
30typedef SDL_Point Pico_Dim;
31typedef SDL_Rect Pico_Rect;
32typedef SDL_Color Pico_Color;
33typedef SDL_Point Pico_Anchor;
34typedef SDL_Point Pico_Flip;
35typedef SDL_Point Pico_Pct;
36
37#define PICO_LEFT 0
38#define PICO_CENTER 50
39#define PICO_RIGHT 100
40#define PICO_TOP 0
41#define PICO_MIDDLE 50
42#define PICO_BOTTOM 100
43
47
52
53#define PICO_SIZE_KEEP ((Pico_Dim) {0,0})
54#define PICO_SIZE_FULLSCREEN ((Pico_Dim) {0,1})
55
56/// @}
57
58/// @defgroup Init
59/// @brief Functions and values used in initialization.
60/// @{
61
62/// @brief Initializes and terminates pico.
63/// @include init.c
64/// @param on 1 to initialize, or 0 to terminate
65void pico_init (int on);
66
67/// @}
68
69/// @defgroup Input
70/// @brief Event handling.
71/// @{
72
73/// @brief Stops the program until the given number of milliseconds have passed.
74/// @include delay.c
75/// @param ms milliseconds to wait
76void pico_input_delay (int ms);
77
78/// @brief Stops the program until an event occurs.
79/// @include event.c
80/// @param evt where to save the event data, or NULL to ignore
81/// @param type type of event to wait for (Pico_EventType)
82/// @sa pico_input_event_ask
83/// @sa pico_input_event_timeout
84void pico_input_event (Pico_Event* evt, int type);
85
86/// @brief Checks if an event has occured.
87/// @param evt where to save the event data, or NULL to ignore
88/// @param type type of event to check the occurence (Pico_EventType)
89/// @return 1 if the given type of event has occurred, or 0 otherwise
90/// @sa pico_input_event
91/// @sa pico_input_event_timeout
92int pico_input_event_ask (Pico_Event* evt, int type);
93
94/// @brief Stops the program until an event occurs or a timeout is reached.
95/// @include event_timeout.c
96/// @param evt where to save the event data, or NULL to ignore
97/// @param type type of event to wait for (Pico_EventType)
98/// @param timeout time limit to wait for (milliseconds)
99/// @return 1 if the given type of event has occurred, or 0 otherwise
100/// @sa pico_input_event
101/// @sa pico_input_event_ask
102int pico_input_event_timeout (Pico_Event* evt, int type, int timeout);
103
104/// @}
105
106/// @defgroup Output
107/// @brief Draw primitives, play sounds, etc.
108/// @{
109
110/// @brief Clears screen with color set by @ref pico_set_color_clear.
112
113/// @brief Draws an RGBA image that is managed by the user.
114/// @param pos drawing coordinate
115/// @param buffer the RGBA image
116/// @param size image size
117/// @sa pico_output_draw_image
118/// @sa pico_output_draw_image_ext
119void pico_output_draw_buffer (Pico_Pos pos, const Pico_Color buffer[], Pico_Dim size);
120
121/// @brief Draws an image.
122/// @param pos drawing coordinate
123/// @param path path to the image file
124/// @sa pico_output_draw_buffer
125/// @sa pico_output_draw_image_ext
126void pico_output_draw_image (Pico_Pos pos, const char* path);
127
128/// @brief Draws an image with the specified size.
129/// @param pos drawing coordinate
130/// @param path path to the image file
131/// @param size image size
132/// @sa pico_output_draw_buffer
133/// @sa pico_output_draw_image
134void pico_output_draw_image_ext (Pico_Pos pos, const char* path, Pico_Dim size);
135
136/// @brief Draws a line.
137/// @param p1 first point
138/// @param p2 second point
140
141/// @brief Draws a single pixel.
142/// @param pos drawing coordinate
144
145/// @brief Draws a batch of pixels.
146/// @param apos array of coordinates
147/// @param count amount of coordinates
148void pico_output_draw_pixels (const Pico_Pos* apos, int count);
149
150/// @brief Draws a rectangle.
151/// @param rect rectangle to draw
153
154/// @brief Draws a triangle with a right angle at bottom-left.
155/// @param rect bounds of the triangle
157
158/// @brief Draws an ellipse.
159/// @param rect bounds of the ellipse
161
162/// @brief Draws a polygon.
163/// @param apos array of coordinates
164/// @param count amount of coordinates
165void pico_output_draw_poly (const Pico_Pos* apos, int count);
166
167/// @brief Draws text.
168/// @param pos drawing coordinate
169/// @param text text to draw
170/// @sa pico_output_draw_text_ext
171void pico_output_draw_text (Pico_Pos pos, const char* text);
172
173/// @brief Draws text with the specified size.
174/// @param pos drawing coordinate
175/// @param text text to draw
176/// @param size text size
177/// @sa pico_output_draw_text
178void pico_output_draw_text_ext (Pico_Pos pos, const char* text, Pico_Dim size);
179
180void pico_output_draw_fmt (Pico_Pos pos, const char* fmt, ...);
181
182void pico_output_draw_fmt_ext (Pico_Pos pos, Pico_Dim size, const char* fmt, ...);
183
184/// @brief Shows what has been drawn onto the screen.
185/// Only does anything on expert mode.
186/// @sa pico_set_expert
188
189/// @brief Takes a screenshot.
190/// @param path screenshot filepath (NULL uses timestamp in the name)
191/// @return The filepath of the screenshot.
192/// @sa pico_output_screenshot_ext
193const char* pico_output_screenshot (const char* path);
194
195/// @brief Takes a screenshot from a specific portion of the screen.
196/// @param path screenshot filepath (NULL uses timestamp in the name)
197/// @param r region to screenshot, in logical coordinates
198/// @return The filepath of the screenshot.
199/// @sa pico_output_screenshot
200const char* pico_output_screenshot_ext (const char* path, Pico_Rect r);
201
202/// @brief Plays a sound.
203/// @param path path to the audio file
204void pico_output_sound (const char* path);
205
206/// @brief Draws text with an internal cursor as reference, like in text editors.
207/// The cursor position updates to (x + len_text * FNT_SIZE, y).
208/// @param text text to draw
209void pico_output_write (const char* text);
210
211/// @brief Draws a line of text with an internal cursor as reference, like in text editors.
212/// The cursor position updates to (x, y + FNT_SIZE).
213/// @param text text to draw
214void pico_output_writeln (const char* text);
215
216/// @}
217
218/// @defgroup State
219/// @brief All getters and setters.
220/// @{
221
222// GET
223
224/// @brief Gets the origin used to draw objects (center, topleft, etc).
225/// @sa pico_get_anchor_rotate
227
228/// @brief Gets the origin used to rotate objects (center, topleft, etc).
229/// @sa pico_get_anchor_draw
231
232/// @brief Gets the color set to clear the screen.
234
235/// @brief Gets the color set to draw.
237
238/// @brief Gets the cropping applied to objects when drawing them.
240
241/// @brief Gets the position of the text cursor.
242/// @sa pico_output_write
243/// @sa pico_output_writeln
245
246/// @brief Gets the state of expert mode.
248
249/// @brief Gets the flipping state of objects.
251
252/// @brief Gets the font used to draw texts.
253const char* pico_get_font (void);
254
255/// @brief Gets the state of the logical pixel grid.
256int pico_get_grid (void);
257
258/// @brief Gets the rotation angle of objects (in degrees).
260
261/// @brief Gets the scaling factor of objects (percentage).
263
264/// @brief Gets the point of view on the logical window.
266
267/// @brief Gets the physical and logical window size.
269
270/// @brief Gets the size of the given image.
271/// @param file image filepath
272Pico_Dim pico_get_size_image (const char* file);
273
274/// @brief Gets the size of the given text.
275/// @param text text to measure
276Pico_Dim pico_get_size_text (const char* text);
277
278/// @brief Gets the visibility state of the window.
279int pico_get_show (void);
280
281/// @brief Gets the drawing style.
283
284/// @brief Gets the amount of ticks that passed since pico was initialized.
285Uint32 pico_get_ticks (void);
286
287/// @brief Gets the aplication title.
288const char* pico_get_title (void);
289
290/// @brief Gets the scaling factor of the screen view (percentage).
292
293// SET
294
295/// @brief Changes the reference to draw objects (center, topleft, etc).
296/// @include anchor.c
297/// @param anchor anchor for the x and y axis
299
300/// @brief Changes the reference to rotate objects (center, topleft, etc).
301/// @include anchor.c
302/// @param anchor anchor for the x and y axis
304
305/// @brief Changes the color used to clear the screen.
306/// @param color new color
308
309/// @brief Changes the color used to draw objects.
310/// @param color new color
312
313/// @brief Changes the cropping that is applied to images before drawing them.
314/// @param crop cropping region, which may have 0 area to disable cropping
316
317/// @brief Sets the position of the text cursor.
318/// @param pos new cursor position
319/// @sa pico_output_write
320/// @sa pico_output_writeln
322
323/// @brief Toggles the expert mode.
324/// @param on 1 to enable it, or 0 to disable it
325void pico_set_expert (int on);
326
327/// @brief Sets the flipping state of objects.
329
330/// @brief Changes the font used to draw texts.
331/// @param file path to font file
332/// @param h size of the font
333void pico_set_font (const char* file, int h);
334
335/// @brief Toggles a grid on top of logical pixels.
336/// @param on 1 to show it, or 0 to hide it
337void pico_set_grid (int on);
338
339/// @brief Sets the rotation angle of objects (in degrees).
340void pico_set_rotate (int angle);
341
342/// @brief Sets the scaling factor of objects
343/// @param scale new scaling for x and y axis (percentage)
345
346/// @brief Sets the point of view on the logical window.
347/// @param pos new point of view
349
350/// @brief Sets the physical and logical window sizes.
351/// @param phy new physical size
352/// @param log new logical size
354
355/// @brief Toggles the aplication window visibility.
356/// @param on 1 to show, or 0 to hide
357void pico_set_show (int on);
358
359/// @brief Sets the drawing style.
360/// @param style new style
362
363/// @brief Sets the aplication title.
364/// @param title new title
365void pico_set_title (const char* title);
366
367/// @brief Sets the scaling factor of the screen view.
368/// @param zoom new scaling for x and y axis (percentage)
370
371/// @}
372
373/// @defgroup Utils
374/// @brief Utilities for users
375/// @{
376
377/// @brief Asserts condition and shows SDL error on failure.
378/// @param x condition to assert
379#define pico_assert(x) if (!(x)) { fprintf(stderr,"%s\n",SDL_GetError()); assert(0 && "SDL ERROR"); }
380
381/// @brief Returns a size relative to the screen size.
382/// @param pct percentage (may be out of [0,100])
383/// @sa pico_dim_ext
385
386/// @brief Returns a size relative to the given rectangle's size.
387/// @param pct percentage (may be out of [0,100])
388/// @param dim the reference rectangle
389/// @sa pico_dim
391
392/// @brief Checks if a point is inside a rectangle.
393/// Assumes that both primitives use the same anchor.
394/// @param pt point
395/// @param r rectangle
396/// @return 1 if pt is inside r, or 0 otherwise
398
399/// @brief Checks if a point is inside a rectangle.
400/// @param pt point
401/// @param r rectangle
402/// @param ap anchor for pt
403/// @param ar anchor for r
404/// @return 1 if pt is inside r, or 0 otherwise
406
407/// @brief Returns a coordinate relative to the screen rectangle.
408/// @param pct percentage (may be out of [0,100])
409/// @sa pico_pos_ext
411
412/// @brief Returns a coordinate relative to the given rectangle's position.
413/// @param pct percentage (may be out of [0,100])
414/// @param r the reference rectangle
415/// @param anc anchor for r
416/// @sa pico_pos
418
419/// @brief Checks if two rectangles overlap.
420/// Assumes that both rectangles use the same anchor.
421/// @param r1 rectangle 1
422/// @param r2 rectangle 2
423/// @return 1 if r1 and r2 overlap, or 0 otherwise
424/// @sa pico_rect_vs_rect_ext
426
427/// @brief Checks if two rectangles with different anchors overlap.
428/// @param r1 rectangle 1
429/// @param r2 rectangle 2
430/// @param a1 anchor for r1
431/// @param a2 anchor for r2
432/// @return 1 if r1 and r2 overlap, or 0 otherwise
433/// @sa pico_pos_vs_rect
435
436/// @}
437
438#ifdef __cplusplus
439}
440#endif
441
442#endif // PICO_H
443
void pico_init(int on)
Initializes and terminates pico.
void pico_input_delay(int ms)
Stops the program until the given number of milliseconds have passed.
int pico_input_event_ask(Pico_Event *evt, int type)
Checks if an event has occured.
int pico_input_event_timeout(Pico_Event *evt, int type, int timeout)
Stops the program until an event occurs or a timeout is reached.
void pico_input_event(Pico_Event *evt, int type)
Stops the program until an event occurs.
void pico_output_clear(void)
Clears screen with color set by pico_set_color_clear.
void pico_output_writeln(const char *text)
Draws a line of text with an internal cursor as reference, like in text editors. The cursor position ...
void pico_output_present(void)
Shows what has been drawn onto the screen. Only does anything on expert mode.
void pico_output_draw_pixels(const Pico_Pos *apos, int count)
Draws a batch of pixels.
void pico_output_draw_image_ext(Pico_Pos pos, const char *path, Pico_Dim size)
Draws an image with the specified size.
void pico_output_draw_text_ext(Pico_Pos pos, const char *text, Pico_Dim size)
Draws text with the specified size.
void pico_output_draw_oval(Pico_Rect rect)
Draws an ellipse.
void pico_output_draw_tri(Pico_Rect rect)
Draws a triangle with a right angle at bottom-left.
const char * pico_output_screenshot(const char *path)
Takes a screenshot.
void pico_output_draw_rect(Pico_Rect rect)
Draws a rectangle.
void pico_output_draw_buffer(Pico_Pos pos, const Pico_Color buffer[], Pico_Dim size)
Draws an RGBA image that is managed by the user.
void pico_output_draw_text(Pico_Pos pos, const char *text)
Draws text.
void pico_output_draw_poly(const Pico_Pos *apos, int count)
Draws a polygon.
const char * pico_output_screenshot_ext(const char *path, Pico_Rect r)
Takes a screenshot from a specific portion of the screen.
void pico_output_sound(const char *path)
Plays a sound.
void pico_output_draw_fmt_ext(Pico_Pos pos, Pico_Dim size, const char *fmt,...)
void pico_output_draw_image(Pico_Pos pos, const char *path)
Draws an image.
void pico_output_draw_line(Pico_Pos p1, Pico_Pos p2)
Draws a line.
void pico_output_draw_fmt(Pico_Pos pos, const char *fmt,...)
void pico_output_draw_pixel(Pico_Pos pos)
Draws a single pixel.
void pico_output_write(const char *text)
Draws text with an internal cursor as reference, like in text editors. The cursor position updates to...
Pico_Dim pico_get_size_image(const char *file)
Gets the size of the given image.
Pico_Size pico_get_size(void)
Gets the physical and logical window size.
void pico_set_flip(Pico_Flip flip)
Sets the flipping state of objects.
void pico_set_color_draw(Pico_Color color)
Changes the color used to draw objects.
Uint32 pico_get_ticks(void)
Gets the amount of ticks that passed since pico was initialized.
Pico_Color pico_get_color_draw(void)
Gets the color set to draw.
void pico_set_style(PICO_STYLE style)
Sets the drawing style.
void pico_set_scroll(Pico_Pos pos)
Sets the point of view on the logical window.
Pico_Dim pico_get_size_text(const char *text)
Gets the size of the given text.
void pico_set_anchor_draw(Pico_Anchor anchor)
Changes the reference to draw objects (center, topleft, etc).
void pico_set_anchor_rotate(Pico_Anchor anchor)
Changes the reference to rotate objects (center, topleft, etc).
void pico_set_crop(Pico_Rect crop)
Changes the cropping that is applied to images before drawing them.
PICO_STYLE pico_get_style(void)
Gets the drawing style.
void pico_set_size(Pico_Dim phy, Pico_Dim log)
Sets the physical and logical window sizes.
int pico_get_show(void)
Gets the visibility state of the window.
void pico_set_title(const char *title)
Sets the aplication title.
void pico_set_expert(int on)
Toggles the expert mode.
Pico_Color pico_get_color_clear(void)
Gets the color set to clear the screen.
void pico_set_font(const char *file, int h)
Changes the font used to draw texts.
Pico_Pct pico_get_zoom(void)
Gets the scaling factor of the screen view (percentage).
void pico_set_zoom(Pico_Pct zoom)
Sets the scaling factor of the screen view.
int pico_get_rotate(void)
Gets the rotation angle of objects (in degrees).
int pico_get_grid(void)
Gets the state of the logical pixel grid.
Pico_Pct pico_get_scale(void)
Gets the scaling factor of objects (percentage).
const char * pico_get_title(void)
Gets the aplication title.
Pico_Pos pico_get_scroll(void)
Gets the point of view on the logical window.
void pico_set_cursor(Pico_Pos pos)
Sets the position of the text cursor.
int pico_get_expert(void)
Gets the state of expert mode.
void pico_set_color_clear(Pico_Color color)
Changes the color used to clear the screen.
void pico_set_grid(int on)
Toggles a grid on top of logical pixels.
Pico_Anchor pico_get_anchor_rotate(void)
Gets the origin used to rotate objects (center, topleft, etc).
Pico_Anchor pico_get_anchor_draw(void)
Gets the origin used to draw objects (center, topleft, etc).
Pico_Flip pico_get_flip(void)
Gets the flipping state of objects.
Pico_Rect pico_get_crop(void)
Gets the cropping applied to objects when drawing them.
void pico_set_rotate(int angle)
Sets the rotation angle of objects (in degrees).
void pico_set_scale(Pico_Pct scale)
Sets the scaling factor of objects.
Pico_Pos pico_get_cursor(void)
Gets the position of the text cursor.
void pico_set_show(int on)
Toggles the aplication window visibility.
const char * pico_get_font(void)
Gets the font used to draw texts.
PICO_STYLE
Definition pico.h:44
SDL_Point Pico_Anchor
Definition pico.h:33
SDL_Point Pico_Flip
Definition pico.h:34
SDL_Point Pico_Pct
Definition pico.h:35
SDL_Point Pico_Dim
Definition pico.h:30
SDL_Rect Pico_Rect
Definition pico.h:31
SDL_Point Pico_Pos
Definition pico.h:29
SDL_Color Pico_Color
Definition pico.h:32
@ PICO_FILL
Definition pico.h:45
@ PICO_STROKE
Definition pico.h:45
int pico_rect_vs_rect(Pico_Rect r1, Pico_Rect r2)
Checks if two rectangles overlap. Assumes that both rectangles use the same anchor.
Pico_Pos pico_pos_ext(Pico_Pct pct, Pico_Rect r, Pico_Anchor anc)
Returns a coordinate relative to the given rectangle's position.
int pico_pos_vs_rect_ext(Pico_Pos pt, Pico_Rect r, Pico_Anchor ap, Pico_Anchor ar)
Checks if a point is inside a rectangle.
Pico_Pos pico_pos(Pico_Pct pct)
Returns a coordinate relative to the screen rectangle.
Pico_Dim pico_dim_ext(Pico_Pct pct, Pico_Dim d)
Returns a size relative to the given rectangle's size.
Pico_Dim pico_dim(Pico_Pct pct)
Returns a size relative to the screen size.
int pico_pos_vs_rect(Pico_Pos pt, Pico_Rect r)
Checks if a point is inside a rectangle. Assumes that both primitives use the same anchor.
int pico_rect_vs_rect_ext(Pico_Rect r1, Pico_Rect r2, Pico_Anchor a2, Pico_Anchor a1)
Checks if two rectangles with different anchors overlap.
Definition pico.h:48
Pico_Dim log
Definition pico.h:50
Pico_Dim phy
Definition pico.h:49