bzr branch
http://gegoxaren.bato24.eu/bzr/%2Bjunk/c_sdl_joypad_ducktape
21
by Gustav Hatvigsson
* added Modeline to (almost) all files. |
1 |
/* c-basic-offset: 2; tab-width: 2; indent-tabs-mode: nil
|
2 |
* vi: set shiftwidth=2 tabstop=2 expandtab:
|
|
3 |
* :indentSize=2:tabSize=2:noTabs=true:
|
|
4 |
*/
|
|
5 |
||
1
by Gustav Hartvigsson
Initial code. |
6 |
#include "Game.h" |
7 |
#include "MousePointer.h" |
|
8 |
#include "GameButton.h" |
|
9 |
#include "game_utils.h" |
|
4
by Gustav Hartvigsson
What I have done: |
10 |
#include "GameObject.h" |
26
by Gustav Hartvigsson
* Fixed the DynamicArray |
11 |
#include "DynamicArray.h" |
25
by Gustav Hartvigsson
* Switched variable names to something more consistant |
12 |
#include "JSParser.h" |
1
by Gustav Hartvigsson
Initial code. |
13 |
|
17
by Gustav Hartvigsson
* fixed a few Doxygen problems. |
14 |
/*
|
7
by Gustav Hartvigsson
* Added licensing information to the files. |
15 |
* This file, as the rest of the project is under MIT license.
|
16 |
* see http://opensource.org/licenses/MIT
|
|
17 |
*
|
|
8
by Gustav Hartvigsson
* added and changed little in the files Lincening information. |
18 |
* Author Gustav Hartvigsson <gustav.hartvigsson _at_ gmail.com> 2014
|
7
by Gustav Hartvigsson
* Added licensing information to the files. |
19 |
*/
|
20 |
||
1
by Gustav Hartvigsson
Initial code. |
21 |
void quit (int rc) { |
22 |
SDL_Quit(); |
|
23 |
exit(rc); |
|
24 |
}
|
|
25 |
||
26 |
void game_button_my_callback_hover (GameButton * self, void * data); |
|
27 |
void game_button_my_callback_stop_hover (GameButton * self, void * data); |
|
28 |
void game_button_my_callback_clicked (GameButton * self, void * data); |
|
29 |
||
30 |
Game * game_new () { |
|
31 |
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) { |
|
32 |
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); |
|
33 |
quit (1); |
|
34 |
} |
|
35 |
|
|
36 |
Game * self = malloc (sizeof (Game)); |
|
37 |
fprintf (stdout, "Creating Game: address: %ld\n", (long) self); |
|
38 |
|
|
39 |
self->window = SDL_CreateWindow ("Hello World", |
|
40 |
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, |
|
41 |
640, 480, SDL_WINDOW_SHOWN); |
|
42 |
|
|
43 |
if (!self->window) { |
|
44 |
fprintf(stderr, "Couldn't create 640x480 window: %s\n", |
|
45 |
SDL_GetError()); |
|
46 |
quit (2); |
|
47 |
} |
|
48 |
|
|
49 |
SDL_ShowCursor(0); |
|
50 |
|
|
51 |
self->renderer = SDL_CreateRenderer (self->window, |
|
52 |
-1, /* initialize the first one supporting the requested flags */ |
|
53 |
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); |
|
54 |
|
|
55 |
if (!self->renderer) { |
|
56 |
fprintf(stderr, "Couldn't create renderer object: %s\n", SDL_GetError()); |
|
57 |
quit (3); |
|
58 |
} |
|
59 |
|
|
60 |
self->game_controller = SDL_GameControllerOpen(0); |
|
61 |
if (!self->game_controller) { |
|
62 |
fprintf(stderr, "Couldn't open controller: %s\n", SDL_GetError()); |
|
63 |
} else { |
|
64 |
fprintf (stdout, "Could open controller %s\n", |
|
65 |
SDL_GameControllerName (self->game_controller)); |
|
66 |
} |
|
67 |
|
|
25
by Gustav Hartvigsson
* Switched variable names to something more consistant |
68 |
self->bg_color_toggle = false; |
69 |
self->background_color = game_color_create_color (0,0,0,255); |
|
1
by Gustav Hartvigsson
Initial code. |
70 |
|
71 |
self->button = game_button_new (50,50, 50, 100); |
|
72 |
game_button_set_callback (self->button, "hover", game_button_my_callback_hover, NULL); |
|
73 |
game_button_set_callback (self->button, "stop_hover", game_button_my_callback_stop_hover, NULL); |
|
74 |
game_button_set_callback (self->button, "clicked", game_button_my_callback_clicked, self); |
|
75 |
|
|
76 |
self->done = 0; |
|
77 |
|
|
78 |
self->mouse_delta.x = 0; |
|
79 |
self->mouse_delta.y = 0; |
|
26
by Gustav Hartvigsson
* Fixed the DynamicArray |
80 |
self->mouse = mouse_pointer_new (640, 480, NULL); |
1
by Gustav Hartvigsson
Initial code. |
81 |
|
82 |
return self; |
|
83 |
} /* game_new */ |
|
84 |
||
85 |
void game_main_loop (Game * self) { |
|
86 |
while (!self->done) { |
|
87 |
game_process_events (self); |
|
88 |
game_draw (self); |
|
89 |
} |
|
90 |
fprintf (stdout, "Exiting main loop!\n"); |
|
91 |
}
|
|
92 |
||
93 |
void game_process_events (Game * self) { |
|
94 |
while (SDL_PollEvent(&self->event)) { |
|
95 |
switch (self->event.type) { |
|
96 |
case SDL_QUIT: |
|
97 |
self->done = 1; |
|
98 |
break; |
|
99 |
case SDL_CONTROLLERAXISMOTION: |
|
100 |
if (self->event.caxis.axis == SDL_CONTROLLER_AXIS_LEFTX) { |
|
101 |
self->mouse_delta.x = self->event.caxis.value / J_SCALE_FACTOR; |
|
102 |
} |
|
103 |
if (self->event.caxis.axis == SDL_CONTROLLER_AXIS_LEFTY) { |
|
104 |
self->mouse_delta.y = self->event.caxis.value / J_SCALE_FACTOR; |
|
105 |
} |
|
106 |
break; |
|
107 |
case SDL_MOUSEBUTTONDOWN: |
|
108 |
case SDL_CONTROLLERBUTTONDOWN: |
|
109 |
if(self->event.cbutton.button == SDL_CONTROLLER_BUTTON_A) { |
|
110 |
fprintf (stdout,"Button A was Pressed!\n"); |
|
111 |
self->mouse->pressed = true; |
|
112 |
} |
|
113 |
break; |
|
114 |
case SDL_MOUSEBUTTONUP: |
|
115 |
case SDL_CONTROLLERBUTTONUP: |
|
116 |
if(self->event.cbutton.button == SDL_CONTROLLER_BUTTON_A) { |
|
117 |
fprintf (stdout,"Button A was Relised!\n"); |
|
118 |
self->mouse->pressed = false; |
|
119 |
} |
|
120 |
break; |
|
121 |
case SDL_MOUSEMOTION: |
|
122 |
mouse_pointer_move_to (self->mouse, |
|
123 |
self->event.motion.x + self->event.motion.xrel, |
|
124 |
self->event.motion.y + self->event.motion.yrel); |
|
125 |
break; |
|
126 |
} |
|
127 |
} |
|
128 |
game_button_check_clicked (self->button, self->mouse); |
|
129 |
game_button_check_hover (self->button, self->mouse); |
|
130 |
mouse_pointer_move (self->mouse, self->mouse_delta.x, self->mouse_delta.y); |
|
131 |
}
|
|
132 |
||
133 |
void game_draw (Game * self) { |
|
25
by Gustav Hartvigsson
* Switched variable names to something more consistant |
134 |
game_color_set_draw_color (self->renderer, self->background_color); |
1
by Gustav Hartvigsson
Initial code. |
135 |
|
136 |
SDL_RenderClear(self->renderer); |
|
137 |
|
|
138 |
game_button_draw (self->button, self->renderer); |
|
139 |
|
|
140 |
mouse_pointer_draw (self->mouse, self->renderer); |
|
141 |
|
|
142 |
SDL_RenderPresent (self->renderer); |
|
143 |
}
|
|
144 |
||
145 |
void game_free (Game * self) { |
|
146 |
fprintf (stdout, "Freeing Game: address: %ld\n", (long) self); |
|
147 |
SDL_DestroyRenderer (self->renderer); |
|
148 |
SDL_DestroyWindow (self->window); |
|
149 |
game_button_free (self->button); |
|
150 |
mouse_pointer_free (self->mouse); |
|
151 |
free (self); |
|
152 |
SDL_Quit (); |
|
153 |
}
|
|
154 |
||
155 |
||
156 |
/* CALLBACKS */
|
|
157 |
||
158 |
void game_button_my_callback_hover (GameButton * self, void * data) { |
|
159 |
fprintf (stdout, "Hover callback is triggered! @ time: %d\n", SDL_GetTicks()); |
|
160 |
game_button_set_fill (self, true); |
|
161 |
}
|
|
162 |
||
163 |
void game_button_my_callback_stop_hover (GameButton * self, void * data) { |
|
164 |
fprintf (stdout, "Stop Hover callback is triggered! @ time: %d\n", SDL_GetTicks()); |
|
165 |
game_button_set_fill (self, false); |
|
166 |
}
|
|
167 |
||
168 |
void game_button_my_callback_clicked (GameButton * self, void * data){ |
|
169 |
Game * game = (Game *) data; |
|
25
by Gustav Hartvigsson
* Switched variable names to something more consistant |
170 |
if (game->bg_color_toggle) { |
171 |
game->bg_color_toggle = false; |
|
172 |
game->background_color = game_color_create_color (0,0,0,255); |
|
1
by Gustav Hartvigsson
Initial code. |
173 |
} else { |
25
by Gustav Hartvigsson
* Switched variable names to something more consistant |
174 |
game->bg_color_toggle = true; |
175 |
game->background_color = game_color_create_color (55,55,55,255); |
|
1
by Gustav Hartvigsson
Initial code. |
176 |
} |
177 |
}
|
|
178 |
||
179 |
||
180 |
||
181 |
/* MOO! */
|