Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Changing Window Size Mid-Game (Like in an options menu) Trouble

This thread is locked; no one can reply to it. rss feed Print
Changing Window Size Mid-Game (Like in an options menu) Trouble
KnightWhoSaysNi
Member #7,339
June 2006
avatar

I am programming a tile map editor. It has two modes; Edit mode to edit the tile map and test mode to display it like it would be in a game. I want to have Edit mode windowed at 1300*800 and test mode full screen at 640*480 but I am having problems doing this. Here some code that demonstrates my problem that uses the classes and similar code in my editor.

1#include <allegro.h>
2#include <iostream>
3 
4#define WINDOWED 0
5#define FULLSCREEN 1
6 
7#define MODE_ONE 1
8#define MODE_TWO 2
9 
10class Window
11{
12 public:
13 Window();
14 Window(int m, int w, int h, int d);
15 ~Window();
16 
17 int ApplySettings();
18 int SetParameters(int m, int w, int h, int d);
19 void Deinitialize();
20 
21 int SetTitle(std::string t);
22 
23 void Flip();
24 void Clear();
25 
26 void ShowMouse();
27 void HideMouse();
28 
29 BITMAP* buffer;
30 
31 private:
32 int window;
33 int width, height;
34 int depth;
35 std::string title;
36 
37 int activePage;
38 BITMAP* page[2];
39};
40 
41Window::Window()
42{
43 window = GFX_AUTODETECT_WINDOWED;
44 
45 width = 640;
46 height = 480;
47 depth = 16;
48 
49 title = "Allegro Window";
50 
51 page[0] = NULL;
52 page[1] = NULL;
53 activePage = 1;
54 
55 allegro_init();
56 
57 install_keyboard();
58 install_mouse();
59 
60}
61 
62Window::Window(int m, int w, int h, int d)
63{
64 if (m == FULLSCREEN)
65 window = GFX_AUTODETECT_FULLSCREEN;
66 else
67 window = GFX_AUTODETECT_WINDOWED;
68 
69 width = w;
70 height = h;
71 depth = d;
72 
73 title = "Allegro Window";
74 
75 page[0] = NULL;
76 page[1] = NULL;
77 activePage = 1;
78 
79 allegro_init();
80 
81 install_keyboard();
82 install_mouse();
83 
84}
85 
86Window::~Window() {}
87 
88int Window::ApplySettings()
89{
90 set_color_depth(depth);
91 
92 if (set_gfx_mode(window, width, height, 0, 0) < 0)
93 return 0;
94 
95 if (page[0] != NULL)
96 destroy_bitmap(page[0]);
97 if (page[1] != NULL)
98 destroy_bitmap(page[1]);
99 
100 page[0] = NULL;
101 page[1] = NULL;
102 
103 page[0] = create_video_bitmap(width, height);
104 page[1] = create_video_bitmap(width, height);
105 
106 buffer = page[activePage];
107 
108 return 1;
109}
110 
111int Window::SetParameters(int m, int w, int h, int d)
112{
113 if (m == FULLSCREEN)
114 window = GFX_AUTODETECT_FULLSCREEN;
115 else
116 window = GFX_AUTODETECT_WINDOWED;
117 
118 width = w;
119 height = h;
120 depth = d;
121}
122 
123int Window::SetTitle(std::string t)
124{
125 title = t;
126 set_window_title(title.c_str());
127 return 0;
128}
129 
130void Window::Deinitialize()
131{
132 destroy_bitmap(page[0]);
133 destroy_bitmap(page[1]);
134}
135 
136void Window::Flip()
137{
138 show_video_bitmap(buffer);
139 
140 activePage = 1 - activePage;
141 
142 buffer = page[activePage];
143}
144 
145void Window::Clear()
146{
147 clear(buffer);
148}
149 
150void Window::ShowMouse()
151{
152 show_os_cursor(MOUSE_CURSOR_ARROW);
153}
154 
155void Window::HideMouse()
156{
157 show_os_cursor(0);
158}
159 
160int main()
161{
162 
163 Window Screen(WINDOWED, 640, 480, 16);
164 Screen.ApplySettings();
165 Screen.ShowMouse();
166 
167 int quit = false;
168 int mode = MODE_ONE;
169 
170 int x = 200;
171 
172 while (!quit)
173 {
174 Screen.Clear();
175 
176 if (key[KEY_ESC])
177 quit = true;
178 
179 if (key[KEY_LEFT])
180 x--;
181 else if (key[KEY_RIGHT])
182 x++;
183 
184 if (mode == MODE_ONE)
185 {
186 if (key[KEY_F2])
187 {
188 mode = MODE_TWO;
189 Screen.SetParameters(WINDOWED, 800, 600, 16);
190 Screen.ApplySettings();
191 }
192 
193 textprintf_ex(Screen.buffer, font, x, 200, makecol(255,255,255), -1, "MODE 1");
194 }
195 
196 else if (mode == MODE_TWO)
197 {
198 if (key[KEY_F1])
199 {
200 mode = MODE_ONE;
201 Screen.SetParameters(WINDOWED, 640, 480, 16);
202 Screen.ApplySettings();
203 }
204 textprintf_ex(Screen.buffer, font, x, 100, makecol(255,255,255), -1, "MODE 2");
205 }
206 
207 Screen.Flip();
208 }
209 
210 Screen.Deinitialize();
211 
212 return 0;
213 
214}END_OF_MAIN()

Upon switching to MODE_TWO the first time by pressing F2 all is fine. Then I switch back to MODE_ONE by pressing F1 and it is fine again. When I switch back to MODE_TWO, it doesn't display the text. MODE_ONE still works fine though.

All I can think of, since I am using page flipping, is that the clear buffer command becomes out of sync.

How do you suggest I change this code or is there an alternative way of doing this?

Thank you for your time.

Go to: