Răsfoiți Sursa

SDL example: tweaks and fixes.

ocornut 10 ani în urmă
părinte
comite
b3ae2976c5

+ 14 - 23
examples/opengl_sdl_example/imgui_impl_sdl.cpp

@@ -1,4 +1,3 @@
-
 #ifdef _MSC_VER
 #include <Windows.h>
 #include <gl/GL.h>
@@ -25,9 +24,6 @@ static GLuint       g_FontTexture = 0;
 // - in your Render function, try translating your projection matrix by (0.5f,0.5f) or (0.375f,0.375f)
 static void ImGui_ImplSdl_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_count)
 {
-    if (cmd_lists_count == 0)
-        return;
-
     // We are using the OpenGL fixed pipeline to make the example code simpler to read!
     // A probable faster way to render would be to collate all vertices from all cmd_lists into a single vertex buffer.
     // Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled, vertex/texcoord/color pointers.
@@ -208,7 +204,7 @@ bool ImGui_ImplSdl_NewFrame(SDL_Window *window)
 
     ImGuiIO& io = ImGui::GetIO();
 
-	bool done(false);
+	bool done = false;
 	SDL_Event event;
 	while (SDL_PollEvent(&event))
 	{
@@ -218,14 +214,15 @@ bool ImGui_ImplSdl_NewFrame(SDL_Window *window)
 			done = true;
 			break;
 		case SDL_MOUSEWHEEL:
-			if (event.wheel.y>0 )
-			{
+			if (event.wheel.y > 0)
 				g_MouseWheel = 1;
-			}
-			if (event.wheel.y<0 )
-			{
+			if (event.wheel.y < 0)
 				g_MouseWheel = -1;
-			}
+			break;
+		case SDL_MOUSEBUTTONDOWN:
+			if (event.button.button == SDL_BUTTON_LEFT) g_MousePressed[0] = true;
+			if (event.button.button == SDL_BUTTON_RIGHT) g_MousePressed[1] = true;
+			if (event.button.button == SDL_BUTTON_MIDDLE) g_MousePressed[2] = true;
 			break;
 		case SDL_TEXTINPUT:
 			ImGui_ImplSdl_CharCallback(event.text.text[0]);
@@ -258,27 +255,21 @@ bool ImGui_ImplSdl_NewFrame(SDL_Window *window)
 
     // Setup inputs
     // (we already got mouse wheel, keyboard keys & characters from glfw callbacks polled in glfwPollEvents())
-	Uint32 windowFlags = SDL_GetWindowFlags(window);
-    if (windowFlags&SDL_WINDOW_MOUSE_FOCUS)
-    {
+    if (SDL_GetWindowFlags(window) & SDL_WINDOW_MOUSE_FOCUS)
     	io.MousePos = ImVec2((float)mx, (float)my);   // Mouse position, in pixels (set to -1,-1 if no mouse / on another screen, etc.)
-    }
     else
-    {
     	io.MousePos = ImVec2(-1,-1);
-    }
    
-    for (int i = 0; i < 3; i++)
-    {
-		io.MouseDown[i] = g_MousePressed[i] || (mouseMask&(1<<i)) != 0;    // If a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame.
-        g_MousePressed[i] = false;
-    }
+	io.MouseDown[0] = g_MousePressed[0] || (mouseMask & SDL_BUTTON(SDL_BUTTON_LEFT)) != 0;		// If a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame.
+	io.MouseDown[1] = g_MousePressed[1] || (mouseMask & SDL_BUTTON(SDL_BUTTON_RIGHT)) != 0;
+	io.MouseDown[2] = g_MousePressed[2] || (mouseMask & SDL_BUTTON(SDL_BUTTON_MIDDLE)) != 0;
+    g_MousePressed[0] = g_MousePressed[1] = g_MousePressed[2] = false;
 
     io.MouseWheel = g_MouseWheel;
     g_MouseWheel = 0.0f;
 
     // Hide/show hardware mouse cursor
-    SDL_ShowCursor( io.MouseDrawCursor ? 0 : 1);
+    SDL_ShowCursor(io.MouseDrawCursor ? 0 : 1);
 
     // Start the frame
     ImGui::NewFrame();

+ 0 - 1
examples/opengl_sdl_example/imgui_impl_sdl.h

@@ -1,4 +1,3 @@
-
 struct		SDL_Window;
 
 bool        ImGui_ImplSdl_Init(SDL_Window *window);

+ 5 - 8
examples/opengl_sdl_example/main.cpp

@@ -1,4 +1,4 @@
-// ImGui - standalone example application for Glfw + OpenGL 2, using fixed pipeline
+// ImGui - standalone example application for SDL2
 
 #include <imgui.h>
 #include "imgui_impl_sdl.h"
@@ -12,10 +12,10 @@
 
 #ifdef MACOSX
 #include <OpenGL/gl.h>
-
 #endif
 
 #include <SDL.h>
+#include <SDL_OpenGL.h>
 
 int SDL_main(int /*argc*/, char* /*argv*/[])
 {
@@ -27,16 +27,13 @@ int SDL_main(int /*argc*/, char* /*argv*/[])
 	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
 	SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
 	SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
-
 	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
 	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
 
-	int width(1024), height(576);
-
 	// SDL window
 	SDL_DisplayMode current;
 	SDL_GetCurrentDisplayMode(0, &current);
-	SDL_Window *window = SDL_CreateWindow( "ImGui OpenGL2/SDL2 example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE );
+	SDL_Window *window = SDL_CreateWindow("ImGui SDL2+OpenGL example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);
   
 	// Create an OpenGL context associated with the window.
 	SDL_GLContext glcontext = SDL_GL_CreateContext(window);
@@ -55,9 +52,9 @@ int SDL_main(int /*argc*/, char* /*argv*/[])
     bool show_another_window = false;
     ImVec4 clear_color = ImColor(114, 144, 154);
 
-	bool done(false);
     // Main loop
-    while(!done)
+	bool done = false;
+    while (!done)
     {
         done = ImGui_ImplSdl_NewFrame(window);