This repository has been archived by the owner on Jun 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
hello_C.c
138 lines (120 loc) · 3.14 KB
/
hello_C.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "GUI_C.h"
#include "GUI_output.h"
/* The button image */
#define IMAGE_FILE "hello.bmp"
/* Variables that we'll need to clean up */
CGUI *gui;
SDL_Surface *image = NULL;
/* Functions that we'll use to implement our interface.
Because we're using C, we can't use the predefined widget classes.
*/
void fill_rectangle(widget_info *info)
{
SDL_Color *rgb;
Uint32 color;
rgb = (SDL_Color *)info->widget_data;
color = SDL_MapRGB(info->screen->format, rgb->r, rgb->g, rgb->b);
SDL_FillRect(info->screen, &info->area, color);
}
void draw_image(widget_info *info)
{
SDL_Surface *image;
image = (SDL_Surface *)info->widget_data;
SDL_BlitSurface(image, NULL, info->screen, &info->area);
}
GUI_status button_quit(widget_info *info, const SDL_Event *event)
{
if ( event->type == SDL_MOUSEBUTTONDOWN ) {
/* We are guaranteed that this event is in our area */
return(GUI_QUIT);
}
return(GUI_PASS);
}
void cleanup(void)
{
if ( image ) {
SDL_FreeSurface(image);
}
GUI_Destroy(gui);
}
void Output(SDL_Window *window, const char *title, const char *text)
{
GUI_Output *output;
output = GUI_CreateOutput(window, 60, 5, NULL);
if ( output ) {
int i, pos;
char formatted_text[1024];
#if 1
/* Center the text in our window */
pos = 0;
formatted_text[pos++] = '\n';
formatted_text[pos++] = '\n';
for ( i=0; i<((60-strlen(text))/2); ++i ) {
formatted_text[pos++] = ' ';
}
formatted_text[pos] = '\0';
strcat(formatted_text, text);
#else
strcpy(formatted_text, text);
#endif
/* Run the output window with our text */
GUI_AddOutput(output, formatted_text);
GUI_ShowOutput(output, 1);
GUI_DeleteOutput(output);
}
}
int main(int argc, char *argv[])
{
SDL_Window *window;
SDL_Surface *screen;
SDL_Color gray;
int x, y;
int error;
CGUI_Widget *widget;
/* Initialize SDL */
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
/* Get a video mode for display */
window = SDL_CreateWindow("GUI Hello!", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, 0);
screen = SDL_GetWindowSurface(window);
/* Create a GUI container */
gui = GUI_Create(window);
/* Add our interface widgets:
We want a gray background and a centered image button.
*/
error = 0;
gray.r = 128;
gray.g = 128;
gray.b = 128;
widget = GUI_Widget_Create(&gray, 0, 0, screen->w, screen->h,
fill_rectangle, NULL, NULL);
error += GUI_AddWidget(gui, widget);
image = SDL_LoadBMP(IMAGE_FILE);
if ( image == NULL ) {
cleanup();
fprintf(stderr, "Couldn't load "IMAGE_FILE": %s\n",
SDL_GetError());
exit(1);
}
x = (screen->w-image->w)/2;
y = (screen->h-image->h)/2;
widget = GUI_Widget_Create(image, x, y, image->w, image->h,
draw_image, button_quit, NULL);
error += GUI_AddWidget(gui, widget);
/* Check to see if there were errors loading the widgets */
if ( error ) {
cleanup();
fprintf(stderr, "Out of memory\n");
}
/* Run the GUI, and then clean up when it's done. */
GUI_Run(gui, NULL);
cleanup();
Output(window, "-= Thanks =-", "Thanks for trying the C GUI interface");
exit(0);
}