root / branches / cpp-ode / src / mmpong-gl / client_gl.cc @ 537

Revision 537, 3.9 kB (checked in by jan@…, 8 months ago)

* massive client code redesign

Line 
1#include "client_gl.h"
2
3using namespace mmp;
4
5ClientGL::ClientGL(std::string _host, std::string _port, std::string _player,
6                   int _win_width, int _win_height, bool _full) :
7                netclient(NetGameCreator::pointer(new NetGameCreatorGL(_player, "/usr/share/mmpong-gl/")), _host, _port),
8                win_width(_win_width), win_height(_win_height), full(_full), grab(false) {
9               
10        //init SDL
11        if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
12                throw runtime_error(string("Renderer: Unable to initialize SDL: ") + SDL_GetError());
13
14        SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
15        SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
16        //SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
17        //SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE, 4);
18        //SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE, 4);
19        //SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, 4);
20        //SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, 4);
21
22        //use SDL_GetVideoInfo call before SDL_SetVideoMode to get screen resolution
23        const SDL_VideoInfo *info = SDL_GetVideoInfo();
24        if (!info)
25                throw runtime_error("ClientGL: Unable to get video info");
26        full_width = info->current_w;
27        full_height = info->current_h;
28        video_flags = SDL_OPENGL | (info->hw_available ? SDL_HWSURFACE : SDL_SWSURFACE);
29
30        SDL_WM_SetCaption("MMPong", NULL);
31        /*SDL_Surface *icon = IMG_Load(Res::get("icon/mmpong.png").c_str());
32        if (icon)
33                SDL_WM_SetIcon(icon, NULL);
34        else
35                cerr << "could not load bitmap: "<< Res::get("icon/mmpong.png") << endl;
36        */
37
38        SDL_ShowCursor(0);
39
40        resize();
41       
42# if !(defined(__APPLE__) || defined(WIN32))
43        renderer.init();
44# endif
45}
46
47ClientGL::~ClientGL() {
48        renderer.quit();
49        SDL_Quit();
50}
51
52void ClientGL::run() {
53
54        SDL_Event event;
55        uint8_t *key_state;
56        uint32_t mouse_state;
57        int mouse_x, mouse_y;
58
59        const boost::posix_time::time_duration framedur=boost::posix_time::milliseconds(1000.0/FRAMERATE);
60       
61        while (netclient.get_con_active()) {
62                NetGame::pointer game=netclient.get_game();
63               
64                while (SDL_PollEvent(&event)) {
65                        if (event.type == SDL_QUIT) {
66                                return;
67                        }
68                        if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_q) {
69                                return;
70                        }                               
71                        if (event.type == SDL_VIDEORESIZE) {
72                                win_width = event.resize.w;
73                                win_height = event.resize.h;
74                                resize();
75                        }
76                        if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_RETURN) {
77                                full = !full;
78                                resize();
79                        }
80                        if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_SPACE) {
81                                grab = !grab;
82                                SDL_WM_GrabInput(grab ? SDL_GRAB_ON : SDL_GRAB_OFF);
83                                SDL_WM_SetCaption(grab ? "MMPong (Press space bar to release pointer)" : "MMPong", NULL);
84                        }                       
85                        if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_HOME) {
86                                renderer.reset_view();
87                        }
88                       
89                }
90               
91                key_state = SDL_GetKeyState(NULL);
92                mouse_state = SDL_GetRelativeMouseState(&mouse_x, &mouse_y);
93
94                if (key_state[SDLK_PAGEUP]) {
95                        renderer.change_view(0.0f, 3.0f*CAM_SPEED);
96                }
97                if (key_state[SDLK_PAGEDOWN]) {
98                        renderer.change_view(0.0f, -3.0f*CAM_SPEED);
99                }
100                if ((mouse_state & SDL_BUTTON(3)) && (mouse_x != 0 || mouse_y != 0)) {
101                        renderer.change_view(mouse_x*CAM_SPEED, mouse_y*CAM_SPEED);
102                }
103                else if (mouse_x != 0 || mouse_y != 0) {
104                        //paddle control here!
105                }
106
107                boost::posix_time::ptime renderbegin = boost::posix_time::microsec_clock::universal_time();
108               
109                renderer.begin();
110                if (game)
111                        game->render();
112                renderer.end();
113               
114                boost::posix_time::ptime renderend = boost::posix_time::microsec_clock::universal_time();
115                boost::posix_time::time_duration renderdur=renderend-renderbegin, waitdur=framedur-renderdur;
116                LOG("Rendering: " << renderdur << " => Waiting " << waitdur);
117                boost::this_thread::sleep(waitdur);
118        }       
119}
120
121void ClientGL::resize() {
122        //gui.prepare();
123       
124        if ((surface = SDL_SetVideoMode(get_width(), get_height(), 0, video_flags | (full ? SDL_FULLSCREEN : SDL_RESIZABLE))) == NULL)
125                throw runtime_error(std::string("ClientGL: Unable to create OpenGL screen: ") + SDL_GetError());
126
127        //gui.resize(get_width(), get_height());
128        renderer.resize(get_width(), get_height());
129}
Note: See TracBrowser for help on using the browser.