summaryrefslogtreecommitdiff
path: root/dumb/dumb-kode54/studio/src/guitop.c
blob: be5cddf9b677c3c60e69f6416efc3096052e67cc (plain)
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
#include <allegro.h>

#include "options.h"
#include "guitop.h"
#include "guiproc.h"
#include "dumbdesk.h"



int the_time;

volatile int true_time;
static int max_time;



static void timer_handler(void)
{
	if (true_time < max_time)
		true_time++;
}
END_OF_STATIC_FUNCTION(timer_handler);



#define MAX_SKIP 20



/*
Every GUI has a reference to its parent - except the top one.

Main loop:
   Test keyboard.
      Esc makes parent GUI active.
      Other keys go to active GUI, which can do as it pleases.
   Test mouse.
      Mouse clicks go to the top GUI.
         The top GUI may pass them down recursively.
            A GUI may be marked as mouse-active, for dragging.
   Update top GUI.
      Update functions are called recursively.
         This can be used for animation for example.
   If there's time, draw top GUI.
      Draw functions only draw areas marked for redrawing.
         Marked areas are unmarked once they have been drawn.
      Draw functions are called recursively.
         If a child has moved:
            It will call its parent for the vacated areas.*

The child will have been removed from the parent's list while
it was drawn, so recursive cycles cannot occur.
*/



void run_desktop(GUI_DESKTOP_PARAM *param)
{
	GUI *gui = gui_create(
		&gui_desktop_commands,
		NULL,
		0, 0, opt.gfx_w, opt.gfx_h,
		param
	);

	if (!gui)
		return;

	gui_active = gui;

	true_time = the_time = 0;
	max_time = MAX_SKIP;

	install_int_ex(timer_handler, BPS_TO_TIMER(100));

	for (;;) {
		gui_draw(gui);
		gui_drawn(gui);

		while (the_time >= true_time)
			yield_timeslice();

		while (the_time < true_time) {
			the_time++;

			/*
			Test keyboard.
				Esc makes parent GUI active.
				Other keys go to active GUI, which can do as it pleases.
			*/
			while (keypressed()) {
				int k = readkey();
				if (k >> 8 == KEY_ESC) {
					if (!gui_active->parent) {
						remove_int(timer_handler);
						return;
					}
					gui_set_active(gui_active->parent);
				} else
					gui_key(gui_active, k);
			}

			/*
			Test mouse.
				Mouse clicks go to the top GUI.
					The top GUI may pass them down recursively.
						A GUI may be marked as mouse-active, for dragging.
			*/

			/*
			Update top GUI.
				Update functions are called recursively.
					This can be used for animation for example.
			*/
			gui_update(gui);
		}

		max_time = the_time + MAX_SKIP;
	}

	gui_destroy(gui);
}



void initialise_guitop(void)
{
	LOCK_FUNCTION(timer_handler);
	LOCK_VARIABLE(true_time);
	LOCK_VARIABLE(max_time);
}