2
#include "MousePointer.h"
3
#include "GameButton.h"
4
#include "game_utils.h"
11
void game_button_my_callback_hover (GameButton * self, void * data);
12
void game_button_my_callback_stop_hover (GameButton * self, void * data);
13
void game_button_my_callback_clicked (GameButton * self, void * data);
16
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
17
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
21
Game * self = malloc (sizeof (Game));
22
fprintf (stdout, "Creating Game: address: %ld\n", (long) self);
24
self->window = SDL_CreateWindow ("Hello World",
25
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
26
640, 480, SDL_WINDOW_SHOWN);
29
fprintf(stderr, "Couldn't create 640x480 window: %s\n",
36
self->renderer = SDL_CreateRenderer (self->window,
37
-1, /* initialize the first one supporting the requested flags */
38
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
40
if (!self->renderer) {
41
fprintf(stderr, "Couldn't create renderer object: %s\n", SDL_GetError());
45
self->game_controller = SDL_GameControllerOpen(0);
46
if (!self->game_controller) {
47
fprintf(stderr, "Couldn't open controller: %s\n", SDL_GetError());
49
fprintf (stdout, "Could open controller %s\n",
50
SDL_GameControllerName (self->game_controller));
53
self->bg_colour_toggle = false;
54
self->background_colour = game_color_create_color (0,0,0,255);
56
self->button = game_button_new (50,50, 50, 100);
57
game_button_set_callback (self->button, "hover", game_button_my_callback_hover, NULL);
58
game_button_set_callback (self->button, "stop_hover", game_button_my_callback_stop_hover, NULL);
59
game_button_set_callback (self->button, "clicked", game_button_my_callback_clicked, self);
63
self->mouse_delta.x = 0;
64
self->mouse_delta.y = 0;
65
self->mouse = mouse_pointer_new (640,480);
70
void game_main_loop (Game * self) {
72
game_process_events (self);
75
fprintf (stdout, "Exiting main loop!\n");
78
void game_process_events (Game * self) {
79
while (SDL_PollEvent(&self->event)) {
80
switch (self->event.type) {
84
case SDL_CONTROLLERAXISMOTION:
85
if (self->event.caxis.axis == SDL_CONTROLLER_AXIS_LEFTX) {
86
self->mouse_delta.x = self->event.caxis.value / J_SCALE_FACTOR;
88
if (self->event.caxis.axis == SDL_CONTROLLER_AXIS_LEFTY) {
89
self->mouse_delta.y = self->event.caxis.value / J_SCALE_FACTOR;
92
case SDL_MOUSEBUTTONDOWN:
93
case SDL_CONTROLLERBUTTONDOWN:
94
if(self->event.cbutton.button == SDL_CONTROLLER_BUTTON_A) {
95
fprintf (stdout,"Button A was Pressed!\n");
96
self->mouse->pressed = true;
99
case SDL_MOUSEBUTTONUP:
100
case SDL_CONTROLLERBUTTONUP:
101
if(self->event.cbutton.button == SDL_CONTROLLER_BUTTON_A) {
102
fprintf (stdout,"Button A was Relised!\n");
103
self->mouse->pressed = false;
106
case SDL_MOUSEMOTION:
107
mouse_pointer_move_to (self->mouse,
108
self->event.motion.x + self->event.motion.xrel,
109
self->event.motion.y + self->event.motion.yrel);
113
game_button_check_clicked (self->button, self->mouse);
114
game_button_check_hover (self->button, self->mouse);
115
mouse_pointer_move (self->mouse, self->mouse_delta.x, self->mouse_delta.y);
118
void game_draw (Game * self) {
119
game_color_set_draw_color (self->renderer, self->background_colour);
121
SDL_RenderClear(self->renderer);
123
game_button_draw (self->button, self->renderer);
125
mouse_pointer_draw (self->mouse, self->renderer);
127
SDL_RenderPresent (self->renderer);
130
void game_free (Game * self) {
131
fprintf (stdout, "Freeing Game: address: %ld\n", (long) self);
132
SDL_DestroyRenderer (self->renderer);
133
SDL_DestroyWindow (self->window);
134
game_button_free (self->button);
135
mouse_pointer_free (self->mouse);
143
void game_button_my_callback_hover (GameButton * self, void * data) {
144
fprintf (stdout, "Hover callback is triggered! @ time: %d\n", SDL_GetTicks());
145
game_button_set_fill (self, true);
148
void game_button_my_callback_stop_hover (GameButton * self, void * data) {
149
fprintf (stdout, "Stop Hover callback is triggered! @ time: %d\n", SDL_GetTicks());
150
game_button_set_fill (self, false);
153
void game_button_my_callback_clicked (GameButton * self, void * data){
154
Game * game = (Game *) data;
155
if (game->bg_colour_toggle) {
156
game->bg_colour_toggle = false;
157
game->background_colour = game_color_create_color (0,0,0,255);
159
game->bg_colour_toggle = true;
160
game->background_colour = game_color_create_color (55,55,55,255);