File: ConsoleExample.c

package info (click to toggle)
libsdl-console 1.3-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 360 kB
  • ctags: 145
  • sloc: ansic: 1,387; sh: 153; makefile: 134
file content (384 lines) | stat: -rw-r--r-- 9,180 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/* This is an example of the console code for SDL
 * Garrett Banuk (mongoose@mongeese.org)
 */

#ifdef GL_DEMO
#include <GL/glut.h>
#endif /* GL_DEMO */

#include <stdlib.h>
#include <string.h>
#include "SDL.h"
#include "CON_console.h"
#include "DT_drawtext.h"
#include "ConsoleExample.h"


int MainProgram = 1;
int TextDemo = 0;
int LargeFont;



#define CONSOLE_N 3
ConsoleInformation *Consoles[CONSOLE_N];/* Pointers to all the consoles */
int ConsoleDisplay[CONSOLE_N];		/* Bools telling if the console is displayed */


int main(int argc, char **argv)
{
	SDL_Surface *Screen;
	int ticks = 0, oldticks = 0;
	int i;
	char framerate[30];
	SDL_Rect Con_rect;



	/* init the graphics */
	if(Init(&Screen, argc, argv))
		return 1;


	/* Init the consoles */
	Con_rect.x = Con_rect.y = 0;
	Con_rect.w = Con_rect.h = 300;
	if((Consoles[0] = CON_Init("ConsoleFont.png", Screen, 100, Con_rect)) == NULL)
		return 1;

	Con_rect.x = 350;
	Con_rect.y = 20;
	Con_rect.w = Con_rect.h = 200;
	if((Consoles[1] = CON_Init("ConsoleFont.png", Screen, 100, Con_rect)) == NULL)
		return 1;

	Con_rect.x = 340;
	Con_rect.y = 280;
	Con_rect.w = 300;
	Con_rect.h = 200;
	if((Consoles[2] = CON_Init("ConsoleFont.png", Screen, 100, Con_rect)) == NULL)
		return 1;


	/* Add some commands to the console */
	CON_AddCommand(&KillProgram, "quit");
	CON_AddCommand(&Echo, "echo");
	CON_AddCommand(&DrawTextDemo, "drawtextdemo");
	CON_AddCommand(&AlphaChange, "alpha");
	CON_AddCommand(&AddBackground, "background");
	CON_AddCommand(&Move, "move");
	CON_AddCommand(&Resize, "resize");
	CON_AddCommand(&ListCommands, "listcommands");
	CON_AddCommand(&SetPrompt, "prompt");

	CON_ListCommands(Consoles[0]);

	/* Heres another font for the text demo */
	LargeFont = DT_LoadFont("LargeFont.png", 0);

	/* Main execution loop */
	while(MainProgram)
	{
		ProcessEvents();

		/* wipe the screen clean with a blue fill and draw the console if its down */
#ifdef GL_DEMO
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
		glTranslatef(0.0, 0.0, -5.0);
		glutSolidTeapot(2.0);
		glFlush();
#else
		SDL_FillRect(Screen, NULL, 255);
#endif /* GL_DEMO */

		if(TextDemo)
			RandText(Screen);

		for(i=0; i<CONSOLE_N; i++)
			if(ConsoleDisplay[i]) CON_DrawConsole(Consoles[i]);

		/* print the framerate */
		oldticks = ticks;
		ticks = SDL_GetTicks();
		sprintf(framerate, "%.2f FPS", 1000.0 / (ticks - oldticks));
		DT_DrawText(framerate, Screen, 1, 1, Screen->h - 40);

#ifdef GL_DEMO
		SDL_GL_SwapBuffers();
#else
		SDL_Flip(Screen);
#endif /* GL_DEMO */
	}

	for(i=0; i<CONSOLE_N; i++)
		CON_Destroy(Consoles[i]);

	return 0;
}

#ifdef GL_DEMO
/* SETUP_OPENGL -- initializes assorted OpenGL parameters */
void setup_opengl(int width, int height)
{
	float ratio = (float)width / (float)height;
	/* lighting init */
	GLfloat mat_diffuse[] = { 0.9, 0.9, 0.0, 1.0 };
	GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
	GLfloat mat_shininess[] = { 50.0 };
	GLfloat light_position[] = { 1.0, 0.0, 5.0, 0.0 };
	GLfloat white_light[] = { 1.0, 1.0, 1.0, 1.0 };

	glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
	glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
	glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
	glLightfv(GL_LIGHT0, GL_POSITION, light_position);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light);
	glLightfv(GL_LIGHT0, GL_SPECULAR, white_light);

	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	glEnable(GL_DEPTH_TEST);

	/* Set the clear color. */
	glClearColor(0.0, 0.0, 1.0, 1.0);

	/* Setup our viewport. */
	glViewport(0, 0, width, height);

	/* Change to the projection matrix and set our viewing volume. */
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(60.0, ratio, 1.0, 1024.0);
}
#endif /* GL_DEMO */

/* Initialise the graphics */
int Init(SDL_Surface **Screen, int argc, char **argv)
{
#ifdef GL_DEMO
	int SetVideoFlags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_OPENGLBLIT;
	int width = 640, height = 480, depth = 24;
#else
	int SetVideoFlags = SDL_HWSURFACE | SDL_DOUBLEBUF;
	int width = 640, height = 480, depth = 16;
#endif /* GL_DEMO */
	int loop;

	for(loop = 1; loop < argc; loop++)
	{
		if(strcmp(argv[loop], "-fullscreen") == 0)
			SetVideoFlags |= SDL_FULLSCREEN;
		else if(strcmp(argv[loop], "-width") == 0)
			width = atoi(argv[++loop]);
		else if(strcmp(argv[loop], "-height") == 0)
			height = atoi(argv[++loop]);
		else if(strcmp(argv[loop], "-bpp") == 0)
			depth = atoi(argv[++loop]);
		else if(strcmp(argv[loop], "-sw") == 0)
			SetVideoFlags |= SDL_SWSURFACE;

	}

	if(SDL_Init(SDL_INIT_VIDEO) < 0)
	{
		fprintf(stderr, "*Error* Couldn't initialize SDL: %s\n", SDL_GetError());
		return 1;
	}

#ifdef GL_DEMO
	SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
	SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6);
	SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
	SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
#endif /* GL_DEMO */

	if((*Screen = SDL_SetVideoMode(width, height, depth, SetVideoFlags)) == NULL)
	{
		fprintf(stderr, "*Error* Couldn't set %dx%dx%d video mode: %s\n", width, height, depth, SDL_GetError());
		SDL_Quit();
		return 1;
	}

#ifdef GL_DEMO
	setup_opengl(width, height);
#endif /* GL_DEMO */

	atexit(SDL_Quit);
	return 0;
}


/* Processes all the incoming events 
 */
void ProcessEvents()
{
	SDL_Event	event;
	static int	console_events = 0; /* True if the console can take the events */


	while(SDL_PollEvent(&event))
	{
		switch(event.type)
		{
			case SDL_KEYDOWN:
				switch(event.key.keysym.sym)
				{
					case SDLK_1:
					case SDLK_2:
					case SDLK_3:
					case SDLK_4:
						if(event.key.keysym.mod & KMOD_CTRL)
						{
							if(ConsoleDisplay[event.key.keysym.sym - SDLK_1])
								ConsoleDisplay[event.key.keysym.sym - SDLK_1] = 0;
							else
								ConsoleDisplay[event.key.keysym.sym - SDLK_1] = 1;
							break;
						}
						else if(event.key.keysym.mod & KMOD_ALT)
						{
							if((event.key.keysym.sym - SDLK_1) == CONSOLE_N)
							{
								CON_Topmost(NULL);
								console_events = 0;
								SDL_EnableUNICODE(0);
								SDL_EnableKeyRepeat(0,0);
							}
							else
							{
								CON_Topmost(Consoles[event.key.keysym.sym - SDLK_1]);
								console_events = 1;
								SDL_EnableUNICODE(1);
								SDL_EnableKeyRepeat(250,30);
							}
							break;
						}
					default:
						/* Send the event to the console */
						if(console_events)
							CON_Events(&event);
						break;
				}
				break;
			case SDL_QUIT:
				MainProgram = 0;
				break;
			default:
				break;
		}
	}
}

/* call this to end the main loop */
void KillProgram(ConsoleInformation *console, char *String)
{
	MainProgram = 0;
}

/* Prints the string you pass it into the console */
void Echo(ConsoleInformation *console, char *String)
{
	CON_Out(console, "%s", String);
}

/* This function toggles the draw text demo */
void DrawTextDemo(ConsoleInformation *console, char *String)
{
	if(TextDemo == 0)
		TextDemo = 1;
	else
		TextDemo = 0;
}

/* This function demonstrates the text drawing routines that
 * come with this console */
void RandText(SDL_Surface *Screen)
{
	DT_DrawText("This is an example of the DrawText routine", Screen, 0, 40, Screen->h - 20);
	DT_DrawText("This is an example of the DrawText routine", Screen, 0, 100, 300);
	DT_DrawText("This is an example of the DrawText routine", Screen, 0, 200, 400);
	DT_DrawText("This is an example of the DrawText routine", Screen, 0, 20, 20);
	DT_DrawText("This is an example of the DrawText routine", Screen, 0, 0, 0);
	DT_DrawText("This is an example of the DrawText routine", Screen, 0, 300, 5);
	DT_DrawText("This is an example of the DrawText routine", Screen, 0, 600, 90);

	/* Now show the large font */
	if(-1 != LargeFont)
	{
		DT_DrawText("Heres a large font (non-transparent)", Screen, LargeFont, 50, 60);
		DT_DrawText("Heres a large font (non-transparent)", Screen, LargeFont, 0, 170);
	}
}

/* lets the user change the alpha level */
void AlphaChange(ConsoleInformation *console, char *alpha)
{
	CON_Alpha(console, atoi(alpha));
	CON_Out(console, "Alpha set to %s.", alpha);
}


/* Adds a background image */
void AddBackground(ConsoleInformation *console, char *string)
{
	int x, y;


	if(2 != sscanf(string, "%d %d", &x, &y))
	{
		x = 0;
		y = 0;
	}

	if(x == -1 || y == -1)
		CON_Background(console, NULL, 0, 0);
	else
		CON_Background(console, "background.jpg", x, y);
}

/* Move the console, takes and x and a y */
void Move(ConsoleInformation *console, char *string)
{
	int x, y;


	if(2 != sscanf(string, "%d %d", &x, &y))
	{
		x = 0;
		y = 0;
	}

	CON_Position(console, x, y);
}

/* resizes the console window, takes and x and y, and a width and height */
void Resize(ConsoleInformation *console, char *string)
{
	int x, y, w, h;

	SDL_Rect rect;
	if(4 != sscanf(string, "%d %d %d %d", &x, &y, &w, &h))
	{
		CON_Out(console, "Usage: X Y Width Height");
		return;
	}
	rect.x = x;
	rect.y = y;
	rect.w = w;
	rect.h = h;
	CON_Resize(console, rect);

}

/* Lists all the commands. */
void ListCommands(ConsoleInformation *console, char *string)
{
	CON_ListCommands(console);
}

void SetPrompt(ConsoleInformation *console, char *string) {
    CON_SetPrompt(console, string);
}