comparison src/main.c @ 19:bc1fdff76775

core: implement most useful events, closes #2445
author David Demelier <markand@malikania.fr>
date Wed, 08 Jan 2020 13:33:41 +0100
parents c91c3272101b
children bc9637a2601b
comparison
equal deleted inserted replaced
18:3ddd3acfe0e9 19:bc1fdff76775
17 */ 17 */
18 18
19 #include <stdio.h> 19 #include <stdio.h>
20 20
21 #include "animation.h" 21 #include "animation.h"
22 #include "clock.h"
23 #include "event.h"
22 #include "font.h" 24 #include "font.h"
23 #include "clock.h"
24 #include "image.h" 25 #include "image.h"
25 #include "sprite.h" 26 #include "sprite.h"
26 #include "texture.h" 27 #include "texture.h"
27 #include "window.h" 28 #include "window.h"
28 29
33 int 34 int
34 main(int argc, char **argv) 35 main(int argc, char **argv)
35 { 36 {
36 (void)argc; 37 (void)argc;
37 (void)argv; 38 (void)argv;
39
38 SDL_Init(SDL_INIT_VIDEO); 40 SDL_Init(SDL_INIT_VIDEO);
39 TTF_Init(); 41 TTF_Init();
40 IMG_Init(IMG_INIT_PNG); 42 IMG_Init(IMG_INIT_PNG);
41 43
42 struct texture *logo, *label;
43 struct sprite sprite;
44 struct clock clock;
45 struct font *font;
46 struct animation animation;
47
48 window_init("Molko's Adventure", 640, 480); 44 window_init("Molko's Adventure", 640, 480);
49 window_set_color(0x667788ff); 45 window_set_color(0x667788ff);
50 46
51 clock_start(&clock); 47 for (;;) {
48 union event ev;
52 49
53 logo = image_openf("E:\\dev\\molko\\explosion.png"); 50 while (event_poll(&ev)) {
54 font = font_openf("E:\\dev\\molko\\DejaVuSans.ttf", 10);
55
56 if (!logo || !font) {
57 printf("%s\n", SDL_GetError());
58 exit(1);
59 }
60
61 label = font_render(font, "Hello World", 0xffffffff);
62 sprite_init(&sprite, logo, 256, 256);
63 animation_init(&animation, &sprite, 20);
64
65 setvbuf(stdout, NULL, _IONBF, 0);
66
67 while (!animation_is_complete(&animation)) {
68 uint64_t ticks = clock_elapsed(&clock);
69
70 clock_start(&clock);
71
72 SDL_Event ev;
73 while (SDL_PollEvent(&ev)) {
74 switch (ev.type) { 51 switch (ev.type) {
75 case SDL_QUIT: 52 case EVENT_QUIT:
76 return 0; 53 return 0;
54 case EVENT_MOUSE:
55 printf("mouse moved to %d, %d, state: %d\n", ev.mouse.x, ev.mouse.y, ev.mouse.buttons);
56 break;
57 case EVENT_CLICKDOWN:
58 printf("mouse click on %d, %d, which: %d\n", ev.click.x, ev.click.y, ev.click.button);
59 break;
60 default:
61 break;
77 } 62 }
78 } 63 }
79 64
80 animation_update(&animation, ticks);
81 window_clear(); 65 window_clear();
82 texture_draw(label, 30, 30);
83 animation_draw(&animation, 10, 10);
84 window_present(); 66 window_present();
85 SDL_Delay(50); 67 SDL_Delay(50);
86 } 68 }
87 69
88 #if 0
89 window_set_color(0xffffffff);
90 window_draw_line(50, 50, 100, 100);
91 window_draw_point(60, 60);
92 window_draw_rectangle(true, 20, 20, 70, 10);
93 logo = image_openf("E:\\Charactervector.png");
94 sprite_init(&sprite, logo, 65, 100);
95 sprite_draw(&sprite, 1, 2, 400, 400);
96 #endif
97
98 return 0; 70 return 0;
99 } 71 }