1 
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 
6 #include "GUI_C.h"
7 #include "GUI_output.h"
8 
9 /* The button image */
10 #define IMAGE_FILE	"hello.bmp"
11 
12 /* Variables that we'll need to clean up */
13 CGUI *gui;
14 SDL_Surface *image = NULL;
15 
16 /* Functions that we'll use to implement our interface.
17    Because we're using C, we can't use the predefined widget classes.
18 */
fill_rectangle(widget_info * info)19 void fill_rectangle(widget_info *info)
20 {
21 	SDL_Color *rgb;
22 	Uint32 color;
23 
24 	rgb = (SDL_Color *)info->widget_data;
25 	color = SDL_MapRGB(info->screen->format, rgb->r, rgb->g, rgb->b);
26 	SDL_FillRect(info->screen, &info->area, color);
27 }
draw_image(widget_info * info)28 void draw_image(widget_info *info)
29 {
30 	SDL_Surface *image;
31 
32 	image = (SDL_Surface *)info->widget_data;
33 	SDL_BlitSurface(image, NULL, info->screen, &info->area);
34 }
button_quit(widget_info * info,const SDL_Event * event)35 GUI_status button_quit(widget_info *info, const SDL_Event *event)
36 {
37 	if ( event->type == SDL_MOUSEBUTTONDOWN ) {
38 		/* We are guaranteed that this event is in our area */
39 		return(GUI_QUIT);
40 	}
41 	return(GUI_PASS);
42 }
43 
cleanup(void)44 void cleanup(void)
45 {
46 	if ( image ) {
47 		SDL_FreeSurface(image);
48 	}
49 	GUI_Destroy(gui);
50 }
51 
Output(SDL_Surface * screen,const char * title,const char * text)52 void Output(SDL_Surface *screen, const char *title, const char *text)
53 {
54 	GUI_Output *output;
55 
56 	output = GUI_CreateOutput(screen, 60, 5, NULL);
57 	if ( output ) {
58 		int i, pos;
59 		char formatted_text[1024];
60 #if 1
61 		/* Center the text in our window */
62 		pos = 0;
63 		formatted_text[pos++] = '\n';
64 		formatted_text[pos++] = '\n';
65 		for ( i=0; i<((60-strlen(text))/2); ++i ) {
66 			formatted_text[pos++] = ' ';
67 		}
68 		formatted_text[pos] = '\0';
69 		strcat(formatted_text, text);
70 #else
71 		strcpy(formatted_text, text);
72 #endif
73 
74 		/* Run the output window with our text */
75 		GUI_AddOutput(output, formatted_text);
76 		GUI_ShowOutput(output, 1);
77 		GUI_DeleteOutput(output);
78 	}
79 }
80 
main(int argc,char * argv[])81 int main(int argc, char *argv[])
82 {
83 	SDL_Surface *screen;
84 	SDL_Color gray;
85 	int x, y;
86 	int error;
87 	CGUI_Widget *widget;
88 
89 	/* Initialize SDL */
90 	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
91 		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
92 		exit(1);
93 	}
94 	atexit(SDL_Quit);
95 
96 	/* Get a video mode for display */
97 	screen = SDL_SetVideoMode(640, 480, 0, SDL_SWSURFACE);
98 	if ( screen == NULL ) {
99 		fprintf(stderr, "Couldn't set video mode: %s\n",SDL_GetError());
100 		exit(1);
101 	}
102 	SDL_WM_SetCaption("GUI Hello!", "hello");
103 
104 	/* Create a GUI container */
105 	gui = GUI_Create(screen);
106 
107 	/* Add our interface widgets:
108 	   We want a gray background and a centered image button.
109 	 */
110 	error = 0;
111 	gray.r = 128;
112 	gray.g = 128;
113 	gray.b = 128;
114 	widget = GUI_Widget_Create(&gray, 0, 0, screen->w, screen->h,
115 					fill_rectangle, NULL, NULL);
116 	error += GUI_AddWidget(gui, widget);
117 	image = SDL_LoadBMP(IMAGE_FILE);
118 	if ( image == NULL ) {
119 		cleanup();
120 		fprintf(stderr, "Couldn't load "IMAGE_FILE": %s\n",
121 						SDL_GetError());
122 		exit(1);
123 	}
124 	x = (screen->w-image->w)/2;
125 	y = (screen->h-image->h)/2;
126 	widget = GUI_Widget_Create(image, x, y, image->w, image->h,
127 					draw_image, button_quit, NULL);
128 	error += GUI_AddWidget(gui, widget);
129 
130 	/* Check to see if there were errors loading the widgets */
131 	if ( error ) {
132 		cleanup();
133 		fprintf(stderr, "Out of memory\n");
134 	}
135 
136 	/* Run the GUI, and then clean up when it's done. */
137 	GUI_Run(gui, NULL);
138 	cleanup();
139 	Output(screen, "-= Thanks =-", "Thanks for trying the C GUI interface");
140 	exit(0);
141 }
142