Exemplo n.º 1
0
bool Window::HandleMessages() const
{
#ifndef UNIX_PORT

  MSG msg;	
  while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 
  {
    if(msg.message == WM_QUIT)
      return false;
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }

#else

  SDL_Event e;
  while(SDL_PollEvent(&e))
  {
    if(TwEventSDL(&e, SDL_MAJOR_VERSION, SDL_MINOR_VERSION) != 0)
    {
      continue;
    }

    if(e.type == SDL_QUIT)
    {
      return false;
    }

    // Forward relevant keys directly to custom input manager.
    switch(e.type)
    {
    case SDL_KEYDOWN:
    case SDL_KEYUP:
      {
        auto key = toupper(e.key.keysym.sym & ~SDLK_SCANCODE_MASK);
        Demo::inputManager->GetInputMessages((e.type == SDL_KEYDOWN) ? WM_KEYDOWN : WM_KEYUP, key);
      }
      break;

    case SDL_MOUSEBUTTONDOWN:
      if(e.button.button == SDL_BUTTON_LEFT || e.button.button == SDL_BUTTON_RIGHT)
      {
        Demo::inputManager->GetInputMessages((e.button.button == SDL_BUTTON_LEFT) ? WM_LBUTTONDOWN : WM_RBUTTONDOWN, 0);
      }
      break;

    case SDL_MOUSEBUTTONUP:
      if(e.button.button == SDL_BUTTON_LEFT || e.button.button == SDL_BUTTON_RIGHT)
      {
        Demo::inputManager->GetInputMessages((e.button.button == SDL_BUTTON_LEFT) ? WM_LBUTTONUP : WM_RBUTTONUP, 0);
      }
      break;
    }
  }

#endif /* UNIX_PORT */

  return true;
}
Exemplo n.º 2
0
bool HandleEvents()
{
	SDL_Event e;
	while (SDL_PollEvent(&e))
	{
		if (TwEventSDL((void*)&e, 2, 0))
			continue;

		switch (e.type)
		{
			case SDL_QUIT:
				return false;
			case SDL_KEYDOWN:
			{
				switch (e.key.keysym.sym)
				{
				case SDLK_ESCAPE:
					return false;
				case SDLK_h:
				{
					camera.camSpeed = 300.0f;

				}break;
				case SDLK_j:
				{
					camera.camSpeed = 0.1f;

				}break;
				}
			} break;
			//Move camera when left mouse button is pressed and mouse is moving!
			case SDL_MOUSEMOTION:
			{
				if (e.motion.state & SDL_BUTTON_LMASK)
				{
					float pitch = (float)e.motion.yrel / 200.0f;
					float yaw = (float)e.motion.xrel / 200.0f;

					camera.Pitch(pitch);
					camera.Yaw(-yaw);
				}
			} break;
		}
	}

	return true;
}
Exemplo n.º 3
0
int main()
{
    SDL_Window *window = NULL;
    int width  = 480, height = 480;
    int flags;
    int quit = 0;

    // Initialize SDL, then get the current video mode and use it to create a SDL window.
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        fprintf(stderr, "Video initialization failed: %s\n", SDL_GetError());
        SDL_Quit();
        exit(1);
    }

    // Request GL context to be OpenGL 3.2 Core Profile
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
    // Other GL attributes
    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
    //SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN;
    window = SDL_CreateWindow("AntTweakBar example using OpenGL Core Profile and SDL",
        SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
        width, height,
        flags);
    if (window == NULL)
    {
        fprintf(stderr, "Video mode set failed: %s\n", SDL_GetError());
        SDL_Quit();
        exit(1);
    }

    // Initialize AntTweakBar
    if (!TwInit(TW_OPENGL_CORE, NULL)) {
        fprintf(stderr, "AntTweakBar initialization failed: %s\n", TwGetLastError());
        SDL_Quit();
        exit(1);
    }
    // Tell the window size to AntTweakBar
    TwWindowSize(width, height);
    // Create a tweak bar
    CreateTweakBar();

    // Set OpenGL viewport
    glViewport(0, 0, width, height);

    // Prepare GL shaders and programs for drawing
    InitRender();

    // Main loop:
    // - Draw scene
    // - Process events
    while (!quit)
    {
        SDL_Event event;
        int handled;
        GLenum error;

        // Clear screen
        glClearColor(0.5f, 0.75f, 0.8f, 1);
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

        // Update angle and draw geometry
        angle = (float)SDL_GetTicks()/25.0f * (FLOAT_PI/180.0f);
        quat[0] = quat[1] = 0;
        quat[2] = (float)sin(angle/2.0f);
        quat[3] = (float)cos(angle/2.0f);
        Render();

        // Draw tweak bars
        TwDraw();

        // Present frame buffer
        SDL_GL_SwapWindow(window);

        // Process incoming events
        while (SDL_PollEvent(&event))
        {
            // Send event to AntTweakBar
            handled = TwEventSDL(&event);

            // If event has not been handled by AntTweakBar, process it
            if (!handled)
            {
                switch (event.type)
                {
                case SDL_QUIT:  // Window is closed
                    quit = 1;
                    break;

                case SDL_WINDOWEVENT:   // Window size has changed
                    // Resize SDL video mode
                    if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
                        width = event.window.data1;
                        height = event.window.data2;

                        // Resize OpenGL viewport
                        glViewport(0, 0, width, height);

                        // Restore OpenGL states
                        InitRender();

                        // TwWindowSize has been called by TwEventSDL,
                        // so it is not necessary to call it again here.

                    }
                    break;
                }
            }
        }

        while ((error = glGetError()) != GL_NO_ERROR)
            fprintf(stderr, "GL error detected: 0x%04X\n", error);

    } // End of main loop

    // Terminate AntTweakBar
    TwTerminate();

    // Delete GL shaders and buffer
    UninitRender();

    // Terminate SDL
    SDL_Quit();

    return 0;
}
int main()
{
    const SDL_VideoInfo* video = NULL;
    int width  = 480, height = 480;
    int bpp, flags;
    int quit = 0;

    // Initialize SDL, then get the current video mode and use it to create a SDL window.
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        fprintf(stderr, "Video initialization failed: %s\n", SDL_GetError());
        SDL_Quit();
        exit(1);
    }
    video = SDL_GetVideoInfo();
    if (!video) 
    {
        fprintf(stderr, "Video query failed: %s\n", SDL_GetError());
        SDL_Quit();
        exit(1);
    }
    // Request GL context to be OpenGL 3.2 Core Profile
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
    // Other GL attributes
    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
    //SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    bpp = video->vfmt->BitsPerPixel;
    flags = SDL_OPENGL | SDL_HWSURFACE;
    if (!SDL_SetVideoMode(width, height, bpp, flags))
    {
        fprintf(stderr, "Video mode set failed: %s\n", SDL_GetError());
        SDL_Quit();
        exit(1);
    }
    SDL_WM_SetCaption("AntTweakBar example using OpenGL Core Profile and SDL", "AntTweakBar+GLCore+SDL");

    // Enable SDL unicode and key-repeat
    SDL_EnableUNICODE(1);
    SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);

    // Load some OpenGL core functions
    if (!LoadGLCoreFunctions())
    {
        fprintf(stderr, "OpenGL 3.2 not supported.\n");
        SDL_Quit();
        exit(1);
    }

    // Initialize AntTweakBar
    if (!TwInit(TW_OPENGL_CORE, NULL)) {
        fprintf(stderr, "AntTweakBar initialization failed: %s\n", TwGetLastError());
        SDL_Quit();
        exit(1);
    }
    // Tell the window size to AntTweakBar
    TwWindowSize(width, height);
    // Create a tweak bar
    CreateTweakBar();

    // Set OpenGL viewport
    glViewport(0, 0, width, height);

    // Prepare GL shaders and programs for drawing
    InitRender();

    // Main loop:
    // - Draw scene
    // - Process events
    while (!quit)
    {
        SDL_Event event;
        int handled;
        GLenum error;

        // Clear screen
        glClearColor(0.5f, 0.75f, 0.8f, 1);
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

        // Update angle and draw geometry
        angle = (float)SDL_GetTicks()/25.0f * (FLOAT_PI/180.0f);
        quat[0] = quat[1] = 0;
        quat[2] = (float)sin(angle/2.0f);
        quat[3] = (float)cos(angle/2.0f);
        Render();

        // Draw tweak bars
        TwDraw();

        // Present frame buffer
        SDL_GL_SwapBuffers();

        // Process incoming events
        while (SDL_PollEvent(&event)) 
        {
            // Send event to AntTweakBar
            handled = TwEventSDL(&event, SDL_MAJOR_VERSION, SDL_MINOR_VERSION);

            // If event has not been handled by AntTweakBar, process it
            if (!handled)
            {
                switch (event.type)
                {
                case SDL_QUIT:  // Window is closed
                    quit = 1;
                    break;

                case SDL_VIDEORESIZE:   // Window size has changed
                    // Resize SDL video mode
                    width = event.resize.w;
                    height = event.resize.h;
                    if (!SDL_SetVideoMode(width, height, bpp, flags))
                        fprintf(stderr, "WARNING: Video mode set failed: %s\n", SDL_GetError());

                    // Resize OpenGL viewport
                    glViewport(0, 0, width, height);
                    
                    // Restore OpenGL states
                    InitRender();
                    
                    // TwWindowSize has been called by TwEventSDL, 
                    // so it is not necessary to call it again here.

                    break;
                }
            }
        }

        while ((error = glGetError()) != GL_NO_ERROR) 
            fprintf(stderr, "GL error detected: 0x%04X\n", error);

    } // End of main loop

    // Terminate AntTweakBar
    TwTerminate();

    // Delete GL shaders and buffer
    UninitRender();

    // Terminate SDL
    SDL_Quit();

    return 0;
}  
Exemplo n.º 5
0
int main(int argc, char *argv[])
{
	const SDL_VideoInfo* video = NULL;
	bool bQuit = false;

	if( g_Screen.initialize() < 0 )
	{
		fprintf(stderr, "Video initialization failed: %s\n", SDL_GetError());
		SDL_Quit();
        exit(1);
    }

    video = SDL_GetVideoInfo();
    if( !video )
	{
		fprintf(stderr, "Video query failed: %s\n", SDL_GetError());
		SDL_Quit();
        exit(1);
    }

    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 1);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

	g_Screen.m_bpp = video->vfmt->BitsPerPixel;

	fprintf(stderr, "BPP %d\n", video->vfmt->BitsPerPixel);
	if (g_Screen.m_bpp == 16)
	{
	    fprintf(stderr, "WARNING:Running at 16bit expecting decreased performance\nChanging Desktop depth to 32bit may correct this problem\n");
	}

	g_Screen.m_flags = SDL_OPENGL | SDL_HWSURFACE | SDL_RESIZABLE;
	g_Screen.m_width = 800;
	g_Screen.m_height = 600;
	g_Screen.m_useVBO = false;

	//flags |= SDL_FULLSCREEN;
	if( !g_Screen.setVideoMode() )
	{
        fprintf(stderr, "Video mode set failed: %s", SDL_GetError());
		SDL_Quit();
		exit(1);
	}
	SDL_WM_SetCaption("Pie Toaster powered by AntTweakBar+SDL", "WZ Stats Tools");
	// Enable SDL unicode and key-repeat
	SDL_EnableUNICODE(1);
	SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);

	// Set OpenGL viewport and states
	glViewport(0, 0, g_Screen.m_width, g_Screen.m_height);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_TEXTURE_2D);
	//glEnable(GL_LIGHTING);
	//glEnable(GL_LIGHT0);	// use default light diffuse and position
	//glEnable(GL_NORMALIZE);
	//glEnable(GL_COLOR_MATERIAL);
	glDisable(GL_CULL_FACE);
	//glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);

	glShadeModel(GL_SMOOTH);

	// Initialize AntTweakBar
	TwInit(TW_OPENGL, NULL);

	/// pie vertex type
	TwStructMember vertice3fMembers[] =
	{
		{ "X",	   TW_TYPE_FLOAT,	offsetof(_vertice_list, vertice.x), "step=0.1" },
		{ "Y",	   TW_TYPE_FLOAT,	offsetof(_vertice_list, vertice.y), "step=0.1" },
		{ "Z",	   TW_TYPE_FLOAT,	offsetof(_vertice_list, vertice.z),	"step=0.1" },
		{ "Selected", TW_TYPE_BOOLCPP,	offsetof(_vertice_list, selected), "" },
	};

	g_tw_pieVertexType = TwDefineStruct("VerticePie", vertice3fMembers, 4, sizeof(_vertice_list), NULL, NULL);

	TwStructMember vector2fMembers[] =
	{
		{ "X",	   TW_TYPE_FLOAT,	offsetof(Vector2f, x), "min=0 max=4096 step=1" },
		{ "Y",	   TW_TYPE_FLOAT,	offsetof(Vector2f, y), "min=0 max=4096 step=1" },
	};

	g_tw_pieVector2fType = TwDefineStruct("Vector2fPie", vector2fMembers, 2, sizeof(Vector2f), NULL, NULL);

	// Tell the window size to AntTweakBar
	TwWindowSize(g_Screen.m_width, g_Screen.m_height);

	iIMDShape *testIMD = NULL;

	//ResMaster = new CResMaster;

	ResMaster.getOGLExtensionString();


	if (ResMaster.isOGLExtensionAvailable("GL_ARB_vertex_buffer_object"))
	{
		g_Screen.initializeVBOExtension();
	}

	ResMaster.cacheGridsVertices();

	ResMaster.readTextureList("pages.txt");

	ResMaster.loadTexPages();

	ResMaster.addGUI();

#ifdef SDL_TTF_TEST
	if (!(ResMaster.initFont() && ResMaster.loadFont("FreeMono.ttf", 12)))
	{
		return 1;
	}
#endif

	//argc = 2;
	//argv[1] = "building1b.pie";

	if (argc < 2)
	{
		fprintf(stderr, "NOTE:no file specified\n");
		//ResMaster.addPie(testIMD, "newpie"); //No need to add new pie for now
	}
	else
	{
		testIMD = iV_ProcessIMD(argv[1]);

		if (testIMD == NULL)
		{
			fprintf(stderr, "no file specified\n creating new one...\n");
			ResMaster.addPie(testIMD, "newpie");
		}
		else
		{
			ResMaster.addPie(testIMD, argv[1]);
		}
	}


	//ResMaster.getPieAt(0)->ToFile("test13.pie");

	OpenFileDialog.m_Up = false;

	while( !bQuit )
	{
		SDL_Event event;
		int handled;

		glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

		// Set OpenGL camera
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		gluPerspective(30, (double)g_Screen.m_width/g_Screen.m_height, 1, 1000);
		gluLookAt(0,0,250, 0,0,0, 0,1,0);

		//updateBars();

		inputUpdate();

        // Process incoming events
		while( SDL_PollEvent(&event) )
		{
			if (OpenFileDialog.m_Up)
			{
				if (event.type == SDL_KEYDOWN)
				{
					Uint16 key = event.key.keysym.sym;
					SDLMod modifier = event.key.keysym.mod;

					if (key == SDLK_BACKSPACE)
					{
						OpenFileDialog.decrementChar();
					}
					else if (key == SDLK_KP_ENTER || key == SDLK_RETURN)
					{
						OpenFileDialog.doFunction();
						OpenFileDialog.deleteTextBox();
						continue;
					}
					else if (key == SDLK_PAUSE ||
							key == SDLK_ESCAPE)
					{
						//cancels current action and closes text box
						//OpenFileDialog.text_pointer = NULL;
						OpenFileDialog.deleteTextBox();
						continue;
					}
					else if (key > 12 && key < 128)
					{
						if (modifier & KMOD_CAPS ||
							modifier & KMOD_LSHIFT ||
							modifier & KMOD_RSHIFT)
						{
							if (key >= 'a' && key <= 'z')
							{
								key = toupper(key);
							}
							else
							{
								key = shiftChar(key);
							}
						}
						OpenFileDialog.incrementChar(key);
					}
					OpenFileDialog.updateTextBox();
					//interrupts input when text box is up
					continue;
				}
			}
			else if (AddSubModelDialog.m_Up)
			{
				if (event.type == SDL_KEYDOWN)
				{
					Uint16 key = event.key.keysym.sym;
					SDLMod modifier = event.key.keysym.mod;

					if (key == SDLK_BACKSPACE)
					{
						AddSubModelDialog.decrementChar();
					}
					else if (key == SDLK_KP_ENTER || key == SDLK_RETURN)
					{
						AddSubModelDialog.doFunction();
						AddSubModelDialog.deleteTextBox();
						continue;
					}
					else if (key == SDLK_PAUSE ||
							key == SDLK_ESCAPE)
					{
						//cancels current action and closes text box
						//OpenFileDialog.text_pointer = NULL;
						AddSubModelDialog.deleteTextBox();
						continue;
					}
					else if (key > 12 && key < 128)
					{
						if (modifier & KMOD_CAPS ||
							modifier & KMOD_LSHIFT ||
							modifier & KMOD_RSHIFT)
						{
							if (key >= 'a' && key <= 'z')
							{
								key = toupper(key);
							}
							else
							{
								key = shiftChar(key);
							}
						}
						AddSubModelDialog.incrementChar(key);
					}
					AddSubModelDialog.updateTextBox();
					//interrupts input when text box is up
					continue;
				}
			}
			else if (AddSubModelFileDialog.m_Up)
			{
				if (event.type == SDL_KEYDOWN)
				{
					Uint16 key = event.key.keysym.sym;
					SDLMod modifier = event.key.keysym.mod;

					if (key == SDLK_BACKSPACE)
					{
						AddSubModelFileDialog.decrementChar();
					}
					else if (key == SDLK_KP_ENTER || key == SDLK_RETURN)
					{
						AddSubModelFileDialog.doFunction();
						AddSubModelFileDialog.deleteTextBox();
						continue;
					}
					else if (key == SDLK_PAUSE ||
							key == SDLK_ESCAPE)
					{
						//cancels current action and closes text box
						//OpenFileDialog.text_pointer = NULL;
						AddSubModelFileDialog.deleteTextBox();
						continue;
					}
					else if (key > 12 && key < 128)
					{
						if (modifier & KMOD_CAPS ||
							modifier & KMOD_LSHIFT ||
							modifier & KMOD_RSHIFT)
						{
							if (key >= 'a' && key <= 'z')
							{
								key = toupper(key);
							}
							else
							{
								key = shiftChar(key);
							}
						}
						AddSubModelFileDialog.incrementChar(key);
					}
					AddSubModelFileDialog.updateTextBox();
					//interrupts input when text box is up
					continue;
				}
			}
			else if (ReadAnimFileDialog.m_Up)
			{
				if (event.type == SDL_KEYDOWN)
				{
					Uint16 key = event.key.keysym.sym;
					SDLMod modifier = event.key.keysym.mod;

					if (key == SDLK_BACKSPACE)
					{
						ReadAnimFileDialog.decrementChar();
					}
					else if (key == SDLK_KP_ENTER || key == SDLK_RETURN)
					{
						ReadAnimFileDialog.doFunction();
						ReadAnimFileDialog.deleteTextBox();
						continue;
					}
					else if (key == SDLK_PAUSE ||
							key == SDLK_ESCAPE)
					{
						//cancels current action and closes text box
						//OpenFileDialog.text_pointer = NULL;
						ReadAnimFileDialog.deleteTextBox();
						continue;
					}
					else if (key > 12 && key < 128)
					{
						if (modifier & KMOD_CAPS ||
							modifier & KMOD_LSHIFT ||
							modifier & KMOD_RSHIFT)
						{
							if (key >= 'a' && key <= 'z')
							{
								key = toupper(key);
							}
							else
							{
								key = shiftChar(key);
							}
						}
						ReadAnimFileDialog.incrementChar(key);
					}
					ReadAnimFileDialog.updateTextBox();
					//interrupts input when text box is up
					continue;
				}
			}
			else if (WriteAnimFileDialog.m_Up)
			{
				if (event.type == SDL_KEYDOWN)
				{
					Uint16 key = event.key.keysym.sym;
					SDLMod modifier = event.key.keysym.mod;

					if (key == SDLK_BACKSPACE)
					{
						WriteAnimFileDialog.decrementChar();
					}
					else if (key == SDLK_KP_ENTER || key == SDLK_RETURN)
					{
						WriteAnimFileDialog.doFunction();
						WriteAnimFileDialog.deleteTextBox();
						continue;
					}
					else if (key == SDLK_PAUSE ||
							key == SDLK_ESCAPE)
					{
						//cancels current action and closes text box
						//OpenFileDialog.text_pointer = NULL;
						WriteAnimFileDialog.deleteTextBox();
						continue;
					}
					else if (key > 12 && key < 128)
					{
						if (modifier & KMOD_CAPS ||
							modifier & KMOD_LSHIFT ||
							modifier & KMOD_RSHIFT)
						{
							if (key >= 'a' && key <= 'z')
							{
								key = toupper(key);
							}
							else
							{
								key = shiftChar(key);
							}
						}
						WriteAnimFileDialog.incrementChar(key);
					}
					WriteAnimFileDialog.updateTextBox();
					//interrupts input when text box is up
					continue;
				}
			}

			if (event.type == SDL_KEYDOWN)
			{
				Uint16 key = event.key.keysym.sym;
				SDLMod modifier = event.key.keysym.mod;

				if ((key == SDLK_KP_ENTER || key == SDLK_RETURN) && modifier & KMOD_LALT)
				{
					// Resize SDL video mode
 					g_Screen.m_flags ^= SDL_FULLSCREEN;
					if( !g_Screen.setVideoMode() )
						fprintf(stderr, "WARNING: Video mode set failed: %s", SDL_GetError());

					// Resize OpenGL viewport
					glViewport(0, 0, g_Screen.m_width, g_Screen.m_height);
					if (ResMaster.isTextureMapperUp())
					{
						glMatrixMode(GL_PROJECTION);
						glLoadIdentity();
						glOrtho(0, g_Screen.m_width, 0, g_Screen.m_height, -1, 1);
						glMatrixMode(GL_MODELVIEW);
						glLoadIdentity();
					}
					else
					{
						// Restore OpenGL states (SDL seems to lost them)
						glEnable(GL_DEPTH_TEST);
						glEnable(GL_TEXTURE_2D);
						gluPerspective(30, (double)g_Screen.m_width/g_Screen.m_height, 1, 1000);
						gluLookAt(0,0,250, 0,0,0, 0,1,0);
						//glEnable(GL_LIGHTING);
						//glEnable(GL_LIGHT0);
						//glEnable(GL_NORMALIZE);
						//glEnable(GL_COLOR_MATERIAL);
					}
					glDisable(GL_CULL_FACE);
					//glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);

					// Reloads texture and bind
					ResMaster.freeTexPages();
					ResMaster.loadTexPages();
					ResMaster.cacheGridsVertices();

					// Flush vbo id's
					Uint32	index;
					for (index = 0;index < ResMaster.m_pieCount;index++)
					{
						ResMaster.getPieAt(index)->flushVBOPolys();
					}

					SDL_Event newEvent;
					newEvent.type = SDL_VIDEORESIZE;
					newEvent.resize.w = g_Screen.m_width;
					newEvent.resize.h = g_Screen.m_height;
					SDL_PushEvent(&newEvent);
				}
			}

			// Send event to AntTweakBar
			handled = TwEventSDL(&event);

			// If event has not been handled by AntTweakBar, process it
			if( !handled )
			{
				switch( event.type )
				{
				case SDL_KEYUP:
				case SDL_KEYDOWN:
					inputKeyEvent(event.key, event.type);
					break;
				case SDL_MOUSEMOTION:
					inputMotionMouseEvent(event.motion);
					break;
				case SDL_MOUSEBUTTONUP:
				case SDL_MOUSEBUTTONDOWN:
					inputButtonMouseEvent(event.button, event.type);
					break;
				case SDL_QUIT:	// Window is closed
					bQuit = true;
					break;
				case SDL_VIDEORESIZE:	// Window size has changed
					// Resize SDL video mode
 					g_Screen.m_width = event.resize.w;
					g_Screen.m_height = event.resize.h;
					if( !g_Screen.setVideoMode() )
						fprintf(stderr, "WARNING: Video mode set failed: %s", SDL_GetError());

					// Resize OpenGL viewport
					glViewport(0, 0, g_Screen.m_width, g_Screen.m_height);
					if (ResMaster.isTextureMapperUp())
					{
						glMatrixMode(GL_PROJECTION);
						glLoadIdentity();
						glOrtho(0, g_Screen.m_width, 0, g_Screen.m_height, -1, 1);
						glMatrixMode(GL_MODELVIEW);
						glLoadIdentity();
					}
					else
					{
						// Restore OpenGL states (SDL seems to lost them)
						glEnable(GL_DEPTH_TEST);
						glEnable(GL_TEXTURE_2D);
						gluPerspective(30, (double)g_Screen.m_width/g_Screen.m_height, 1, 1000);
						gluLookAt(0,0,250, 0,0,0, 0,1,0);
						//glEnable(GL_LIGHTING);
						//glEnable(GL_LIGHT0);
						//glEnable(GL_NORMALIZE);
						//glEnable(GL_COLOR_MATERIAL);
					}
					glDisable(GL_CULL_FACE);
					//glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);

					// Reloads texture and bind
					ResMaster.freeTexPages();
					ResMaster.loadTexPages();
					ResMaster.cacheGridsVertices();

					// Flush vbo id's
					Uint32	index;
					for (index = 0;index < ResMaster.m_pieCount;index++)
					{
						ResMaster.getPieAt(index)->flushVBOPolys();
					}

					// TwWindowSize has been called by TwEventSDL, so it is not necessary to call it again here.
					break;
				}
			}
			else
			{
				// Resets all keys and buttons
				inputInitialize();
				// Input in TwBars rebuilds vbo's
				switch( event.type )
				{
					case SDL_KEYUP:
					case SDL_KEYDOWN:
					case SDL_MOUSEMOTION:
					case SDL_MOUSEBUTTONUP:
					case SDL_MOUSEBUTTONDOWN:
						// Flush vbo id's
						Uint32	index;
						for (index = 0;index < ResMaster.m_pieCount;index++)
						{
							CPieInternal	*temp = ResMaster.getPieAt(index);
							if (temp)
							{
								temp->flushVBOPolys();
							}
						}
						break;
					default:
						break;
				}
			}
		}

		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();

		glTranslatef(0.0f,0.0f,0.0f);

		glColor4ub(255, 255, 255, 255);

		ResMaster.logic();
		ResMaster.draw();
		ResMaster.updateInput();

		// Draw tweak bars
		TwDraw();

		// Present frame buffer
		SDL_GL_SwapBuffers();

		SDL_Delay(10);
	}

	// Remove TW bars
	//removeBars();

	// Terminate AntTweakBar
	TwTerminate();

	// Terminate SDL
	SDL_Quit();

	ResMaster.~CResMaster();

	return 1;
}
Exemplo n.º 6
0
int main(int argc, char *argv[])
{
	//Make sure the program waits for a quit
	bool running = true;
	bool step = false;
	bool quit = false;
	SDL_MouseMotionEvent mme;
	init();
	Render *pRender = Render::GetInstance();
	pRender->init_GL();

	// We grab the input so that we receive mouse movement
	// even when the mouse is beyond the edge of the screen
	SDL_ShowCursor(0);
	SDL_WM_GrabInput( SDL_GRAB_ON );

#if TWEAK_MENU
	TwInit(TW_OPENGL, NULL);
	TwWindowSize(SCREEN_W, SCREEN_H);
	myBar = TwNewBar("Tweak");
#endif // TWEAK_MENU

	// Initialize the FPS tracking variables
	Uint32 nextFPSCount = SDL_GetTicks() + 1000;
	int frames = 0;
	int prevTime = SDL_GetTicks();

	Simulation *pSimulation = Simulation::GetSimulation();
	pSimulation->Init();
	while( quit == false )
	{
		while( SDL_PollEvent( &event ) )
		{
#if TWEAK_MENU
			int handled = TwEventSDL(&event, SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
			if( handled )
				continue;
#endif // TWEAK_MENU
			switch( event.type )
			{
			case SDL_QUIT:
				quit = true;
				break;

			case SDL_MOUSEMOTION:
				mme = event.motion;
				pSimulation->UpdateMouseMotion( mme );
				break;

			case SDL_KEYDOWN:
				pSimulation->UpdateKeys(event.key.keysym.sym, true);
				switch( event.key.keysym.sym )
				{
				case SDLK_1:
					break;
				case SDLK_RIGHT:
					step = true;
					break;
				case SDLK_ESCAPE:
					quit=true;
					break;
				}
				break;
			case SDL_KEYUP:
				pSimulation->UpdateKeys(event.key.keysym.sym, false);
				switch( event.key.keysym.sym )
				{
				case SDLK_RIGHT:
					break;
				}
				break;
			}
		}

		if( running || step )
		{
			step = false;
			StartFrame();

			int newTime = SDL_GetTicks();
			float elapsed = (float)(newTime - prevTime) / 1000.0f;
			if( elapsed > 0.05f )
				elapsed = 0.05f;	// Clamp long dt's down
			if( elapsed < 0.001f)
				continue;			// dt too short. Skip to next frame (without updating prevTime so dt grows).
			prevTime = newTime;

			pSimulation->DrawFrame();				// Render all sprites

			const int subSteps = 1;
			for( int i=0; i<subSteps; i++ )
			{
				pSimulation->SimulateOneFrame(elapsed / subSteps);	// Logic update for Simulation
			}

#if TWEAK_MENU
			TwDraw();
#endif // TWEAK_MENU

			EndFrame();

			// Once per second, show the FPS
			frames++;
			if( SDL_GetTicks() >  nextFPSCount )
			{
				char buffer[256];
				sprintf_s(buffer, "FPS: %d", frames );
				SDL_WM_SetCaption( buffer, NULL );
				nextFPSCount = SDL_GetTicks() + 1000;
				frames = 0;
			}
		}
	}
	pSimulation->Shutdown();
	clean_up();
	return 0;
}
Exemplo n.º 7
0
/*
 * Ui_Event
 *
 * Handles input events, returning true if the event was swallowed by TwBar.
 */
boolean_t Ui_Event(SDL_Event *event) {
	return TwEventSDL(event, SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
}