1 /*
2 * Copyright (C) 2004 Tom Bradley
3 * tojabr@shiftygames.com
4 *
5 * file: ShiftyEngine.c
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 *
21 */
22
23 #include "SDL_mixer.h"
24 #include "SDL_image.h"
25 #include "SDL_ttf.h"
26 #include "SDL.h"
27
28 #include <stdarg.h>
29 #include <assert.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <time.h>
33
34 #include "ShiftyEngine.h"
35
36 SDL_Surface * screen = 0;
37 SDL_Surface * background = 0;
38 SDL_Surface * soundControl = 0;
39
40 static char * gameName;
41 static char * backgroundName;
42 static char * fontName;
43 static int fontSize;
44 int volume = 230;
45
46 TTF_Font * mainFont;
47
48 struct SE_Button * se_buttonList = 0;
49 int se_true = 0;
50 int se_false = 1;
51
52 extern void SE_CheckEvents();
53
54 int SOUND_X = 0;
55 int SOUND_Y = 0;
56
57 /*****************************************************
58 ****************************************************/
SE_SetName(char * name)59 void SE_SetName(char * name)
60 {
61 int len = strlen(name);
62 assert(name);
63
64 gameName = (char *)malloc((sizeof(char) * len) + 1);
65 if(!gameName) {
66 fprintf(stderr, "Out Of Memory.");
67 exit(1);
68 }
69
70 strncpy(gameName, name, len);
71 }
72
73 /*****************************************************
74 ****************************************************/
SE_Quit()75 void SE_Quit()
76 {
77 SDL_FreeSurface(screen);
78 SDL_FreeSurface(background);
79
80 exit(1);
81 }
82
83 /*****************************************************
84 ****************************************************/
SE_SetBackground(char * name)85 void SE_SetBackground(char * name)
86 {
87 int len = strlen(name);
88 assert(name);
89
90 backgroundName = (char *)malloc((sizeof(char) * len) + 1);
91 if(!backgroundName) {
92 fprintf(stderr, "Out Of Memory.");
93 exit(1);
94 }
95
96 strncpy(backgroundName, name, len);
97 }
98
99 /*****************************************************
100 ****************************************************/
SE_Init()101 int SE_Init()
102 {
103 putenv("SDL_VIDEO_CENTERED=1");
104
105 /* Init the system */
106 fprintf(stdout, "Initiating SDL...\n");
107 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER ) != 0) {
108 fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError());
109 return 1;
110 }
111
112 fprintf(stdout, "Initiating Audio Mixer...\n");
113 initMixer();
114
115 SDL_WM_SetCaption(gameName, gameName);
116
117 if(TTF_Init() < 0) {
118 fprintf(stderr, "TTF_Init: %s\n", TTF_GetError());
119 return 2;
120 }
121
122 screen = SDL_SetVideoMode(800, 600, 32, SDL_ANYFORMAT);
123 if (screen == NULL) {
124 fprintf(stderr, "Unable to set video mode: %s\n", SDL_GetError());
125 return 1;
126 }
127
128 srandom(time(0));
129
130 atexit(TTF_Quit);
131 atexit(SDL_Quit);
132
133 initLogo(screen);
134
135 soundControl = loadPNG("pics/sound_high.png");
136 setVolume(volume);
137
138 background = loadPNGDisplay(backgroundName);
139
140 return 0;
141 }
142
143
144 /*****************************************************
145 ****************************************************/
SE_Error(char * fmt,...)146 void SE_Error(char * fmt, ...)
147 {
148 va_list ap;
149 int i;
150 char buf[1024];
151
152 va_start(ap, fmt);
153 i = vsprintf(buf, fmt, ap);
154 va_end(ap);
155
156 fprintf(stderr, "ShiftyEngine Error: %s\n", buf);
157 }
158
159 /*****************************************************
160 ****************************************************/
SE_GameLoop()161 void SE_GameLoop()
162 {
163 while(1)
164 SE_CheckEvents();
165 }
166
167
168 /*****************************************************
169 ****************************************************/
SE_RegisterButton(char * name,int x,int y,int w,int h,int * cond,void (* handle)())170 void SE_RegisterButton(char * name, int x, int y, int w, int h, int * cond, void (*handle)())
171 {
172 struct SE_Button * ph, * head, * t = (struct SE_Button *)malloc(sizeof(struct SE_Button));
173 if(t == NULL) {
174 fprintf(stderr, "Out of Memory.");
175 exit(1);
176 }
177
178 strncpy(t->name, name, 16);
179 t->x = x;
180 t->y = y;
181 t->w = w;
182 t->h = h;
183 t->handle = handle;
184 t->over = 0;
185 t->cond = cond;
186 t->next = NULL;
187
188 if(!se_buttonList) {
189 se_buttonList = t;
190 return;
191 }
192
193 head = se_buttonList;
194 while(head) {
195 ph = head;
196 head = head->next;
197 }
198
199 ph->next = t;
200 }
201
202 /*****************************************************
203 ****************************************************/
SE_AdjustSoundLevel()204 void SE_AdjustSoundLevel()
205 {
206 SDL_FreeSurface(soundControl);
207
208 switch(volume) {
209 case 230:
210 volume = 50;
211 soundControl = loadPNG("pics/sound_medium.png");
212 break;
213 case 50:
214 volume = 1;
215 soundControl = loadPNG("pics/sound_low.png");
216 break;
217 case 1:
218 volume = 230;
219 soundControl = loadPNG("pics/sound_high.png");
220 break;
221 }
222
223 setVolume(volume);
224 }
225
226
227 /*****************************************************
228 ****************************************************/
SE_ShowSoundIcon()229 void SE_ShowSoundIcon()
230 {
231 SDL_Rect dest;
232
233 dest.x = SOUND_X;
234 dest.y = SOUND_Y;
235 if(SDL_BlitSurface(soundControl, 0, screen, &dest) != 0)
236 SE_Error("A blit failed.");
237 }
238