File: displaymodes.cpp

package info (click to toggle)
clanlib 0.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 28,372 kB
  • ctags: 16,520
  • sloc: cpp: 101,145; sh: 8,752; xml: 6,410; makefile: 1,740; ansic: 463; perl: 424; php: 247
file content (110 lines) | stat: -rw-r--r-- 2,912 bytes parent folder | download | duplicates (7)
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
#include <ClanLib/core.h>
#include <ClanLib/gui.h>
#include <ClanLib/guistylesilver.h>
#include <ClanLib/application.h>
#include <ClanLib/display.h>
#include <ClanLib/gl.h>

// The main application
class App : public CL_ClanApplication
{
public:
	int main(int argc, char** argv)
	{
		// Create a console window for text-output if not available
		CL_ConsoleWindow console("Console",80,1000);
		console.redirect_stdio();

		try
		{
			CL_SetupCore setup_core;
			CL_SetupDisplay setup_display;
			CL_SetupGL setup_gl;
			CL_SetupGUI setup_gui;

			CL_DisplayWindow display("DisplayModes example", 640, 480);
			
			// Create the GUI using the default style
			CL_ResourceManager gui_resources("../../Resources/GUIStyleSilver/gui.xml");
			CL_StyleManager_Silver style(&gui_resources);
			CL_GUIManager gui(&style);

			window = new CL_Window(CL_Rect(0, 0, 320, 200), "Mode selector", &gui, &style);
			listbox = new CL_ListBox(CL_Rect(10, 10, 140, 164), window->get_client_area());
			button = new CL_Button(CL_Rect(154, 144, 302, 164), "Apply", window->get_client_area());
			new CL_Label(CL_Rect(154, 10, 302, 30), "New mode:", window->get_client_area());
			label = new CL_Label(CL_Rect(164, 30, 302, 50), "Not selected", window->get_client_area());
			checkbox = new CL_CheckBox(CL_Point(164, 50), "Fullscreen", window->get_client_area());
			
			std::vector<CL_DisplayMode> &modes = CL_DisplayMode::get_display_modes();
			for(unsigned int i=0; i<modes.size(); ++i)
			{
				listbox->insert_item(modes[i].get_string());
			}
			
			CL_Slot slot_listbox = listbox->sig_highlighted().connect(this, &App::on_listbox);
			CL_Slot slot_apply = button->sig_clicked().connect(this, &App::on_apply);

			// Connect the Window close event
			CL_Slot slot_quit = display.sig_window_close().connect(this, &App::on_window_close);
			quit = false;
			
			// Main loop
			while (!CL_Keyboard::get_keycode(CL_KEY_ESCAPE) && !quit)
			{
				CL_Display::clear();
				gui.show();

				CL_System::keep_alive();
				CL_Display::flip();
			}
		}
		catch (CL_Error e)
		{
 			std::cout << e.message.c_str() << std::endl;

			// Display console close message and wait for a key
			console.display_close_message();
		}

		return 0;
	}

private:	
	void on_window_close()
	{
		quit = true;
	}

	void on_listbox(int selection)
	{
		label->set_text(listbox->get_current_text());
	}
	
	void on_apply()
	{
		if(listbox->get_current_item() != -1)
		{
			std::vector<CL_DisplayMode> &modes = CL_DisplayMode::get_display_modes();

			if(checkbox->is_checked())
			{
				CL_Display::set_fullscreen(modes[listbox->get_current_item()]);
			}
			else
			{
				CL_Size res = modes[listbox->get_current_item()].get_resolution();
				CL_Display::set_windowed();
				CL_Display::set_size(res.width, res.height);
			}
		}
	}
	
	bool quit;

	CL_Window *window;
	CL_ListBox *listbox;
	CL_Label *label;
	CL_Button *button;
	CL_CheckBox *checkbox;
} app;