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
|
#include "saffron_api.h"
#include "saffron_layout.h"
#include <stdio.h>
#include <saffron.h>
#include <SDL3/SDL.h>
#include <SDL3/SDL_rect.h>
#include <SDL3/SDL_render.h>
/* PLEASE don't do this in production!
* when wrappers are implemented,
* THEY will make draw functions for you
* THIS IS A BAD IDEA */
void my_test_draw(SaffronWidget* self, SDL_Renderer* renderer) {
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_FRect rect = {self->x, self->y, self->w, self->h};
SDL_RenderFillRect(renderer, &rect);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderRect(renderer, &rect);
}
/* YET AGAIN DONT DO THIS IN PROD PLEASE
* this is very lunatic */
void my_test_onclick(SaffronWidget* self) {
printf("clicked!\n");
}
int main(void) {
saffron_init();
SaffronWindow* window = saffron_window_new("Saffron Test", 800, 600);
/* replace the root with our own, because we want horizontal */
saffron_widget_free(window->root);
window->root = (SaffronWidget*)saffron_box_new(SAFFRON_ORIENTATION_HORIZONTAL, SAFFRON_HALIGN_LEFT, SAFFRON_VALIGN_TOP, 5, 5, 0, window->w, window->h);
/* i guess IM THE LUNATIC NOW
* DEAL WITH IT */
SaffronWidget* test = saffron_widget_new();
/* we don't need to set X and Y, the window will layout them automatically */
test->w = 200;
test->h = 150;
test->draw = my_test_draw;
test->on_click = my_test_onclick;
/* make a vertical box */
SaffronWidget* box = (SaffronWidget*)saffron_box_new(SAFFRON_ORIENTATION_VERTICAL, SAFFRON_HALIGN_LEFT, SAFFRON_VALIGN_TOP, 5, 0, 0, 200, 150);
/* lunatic method 2 */
SaffronWidget* test2 = saffron_widget_new();
test2->w = 200;
test2->h = 72;
test2->draw = my_test_draw;
/* lunatic method 3 */
SaffronWidget* test3 = saffron_widget_new();
test3->w = 200;
test3->h = 73;
test3->draw = my_test_draw;
saffron_widget_add_child(window->root, test);
saffron_widget_add_child(window->root, box);
saffron_widget_add_child(box, test2);
saffron_widget_add_child(box, test3);
saffron_window_main(window);
saffron_window_free(window);
saffron_quit();
return 0;
}
|