/+junk/c_sdl_joypad

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/%2Bjunk/c_sdl_joypad

« back to all changes in this revision

Viewing changes to Game.c

  • Committer: Gustav Hartvigsson
  • Date: 2013-09-27 18:52:09 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20130927185209-x6tnykj2lfj0qxwt
Initial code.
This is just an experiment on different techniques that can be used.
It is pseudo object orientated. And has a few util functions to make life
easier.

All code is provided as is. Copy, Learn, Refactor, Have Fun!

TODO: make the MousePointer use an image and add an object drawing lists.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "Game.h"
 
2
#include "MousePointer.h"
 
3
#include "GameButton.h"
 
4
#include "game_utils.h"
 
5
 
 
6
void quit (int rc) {
 
7
  SDL_Quit();
 
8
  exit(rc);
 
9
}
 
10
 
 
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);
 
14
 
 
15
Game * game_new () {
 
16
  if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
 
17
    fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
 
18
    quit (1);
 
19
  }
 
20
  
 
21
  Game * self = malloc (sizeof (Game));
 
22
  fprintf (stdout, "Creating Game: address: %ld\n", (long) self);
 
23
  
 
24
  self->window = SDL_CreateWindow ("Hello World",
 
25
                             SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
 
26
                             640, 480, SDL_WINDOW_SHOWN);
 
27
  
 
28
  if (!self->window) {
 
29
    fprintf(stderr, "Couldn't create 640x480 window: %s\n",
 
30
            SDL_GetError());
 
31
    quit (2);
 
32
  }
 
33
  
 
34
  SDL_ShowCursor(0);
 
35
  
 
36
  self->renderer = SDL_CreateRenderer (self->window,
 
37
          -1, /* initialize the first one supporting the requested flags */
 
38
          SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
 
39
  
 
40
  if (!self->renderer) {
 
41
    fprintf(stderr, "Couldn't create renderer object: %s\n", SDL_GetError());
 
42
    quit (3);
 
43
  }
 
44
  
 
45
  self->game_controller = SDL_GameControllerOpen(0);
 
46
  if (!self->game_controller) {
 
47
    fprintf(stderr, "Couldn't open controller: %s\n", SDL_GetError());
 
48
  } else {
 
49
    fprintf (stdout, "Could open controller %s\n",
 
50
             SDL_GameControllerName (self->game_controller));
 
51
  }
 
52
  
 
53
  self->bg_colour_toggle = false;
 
54
  self->background_colour = game_color_create_color (0,0,0,255);
 
55
  
 
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);
 
60
  
 
61
  self->done = 0;
 
62
  
 
63
  self->mouse_delta.x = 0;
 
64
  self->mouse_delta.y = 0;
 
65
  self->mouse = mouse_pointer_new (640,480);
 
66
  
 
67
  return self;
 
68
} /* game_new */
 
69
 
 
70
void game_main_loop (Game * self) {
 
71
  while (!self->done) {
 
72
    game_process_events (self);
 
73
    game_draw (self);
 
74
  }
 
75
  fprintf (stdout, "Exiting main loop!\n");
 
76
}
 
77
 
 
78
void game_process_events (Game * self) {
 
79
  while (SDL_PollEvent(&self->event)) {
 
80
    switch (self->event.type) {
 
81
        case SDL_QUIT: 
 
82
        self->done = 1;
 
83
        break;
 
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;
 
87
        }
 
88
        if (self->event.caxis.axis == SDL_CONTROLLER_AXIS_LEFTY) {
 
89
         self->mouse_delta.y = self->event.caxis.value / J_SCALE_FACTOR;
 
90
        }
 
91
        break;
 
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;
 
97
        }
 
98
        break;
 
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;
 
104
        }
 
105
        break;
 
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);
 
110
        break;
 
111
    }
 
112
  }
 
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);
 
116
}
 
117
 
 
118
void game_draw (Game * self) {
 
119
  game_color_set_draw_color (self->renderer, self->background_colour);
 
120
  
 
121
  SDL_RenderClear(self->renderer);
 
122
  
 
123
  game_button_draw (self->button, self->renderer);
 
124
  
 
125
  mouse_pointer_draw (self->mouse, self->renderer);
 
126
  
 
127
  SDL_RenderPresent (self->renderer);
 
128
}
 
129
 
 
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);
 
136
  free (self);
 
137
  SDL_Quit ();
 
138
}
 
139
 
 
140
 
 
141
/* CALLBACKS */
 
142
 
 
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);
 
146
}
 
147
 
 
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);
 
151
}
 
152
 
 
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);
 
158
  } else {
 
159
    game->bg_colour_toggle = true;
 
160
    game->background_colour = game_color_create_color (55,55,55,255);
 
161
  }
 
162
}
 
163
 
 
164
 
 
165
 
 
166
/* MOO! */