Rename main.c to sdl2-sdlrenderer.c and implement sdl2-opengl3 example
This commit is contained in:
parent
0658fcf39d
commit
eef16a695e
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,5 +1,6 @@
|
|||||||
.cache
|
.cache
|
||||||
compile_commands.json
|
compile_commands.json
|
||||||
main
|
sdl2-sdlrenderer
|
||||||
|
sdl2-opengl3
|
||||||
extern
|
extern
|
||||||
imgui.ini
|
imgui.ini
|
||||||
|
9
compile
9
compile
@ -3,7 +3,10 @@
|
|||||||
CC=clang
|
CC=clang
|
||||||
CFLAGS="-g -Wall $(pkg-config --cflags sdl2) -Wl,-rpath,\$ORIGIN/lib -Ivendor"
|
CFLAGS="-g -Wall $(pkg-config --cflags sdl2) -Wl,-rpath,\$ORIGIN/lib -Ivendor"
|
||||||
LIBS="$(pkg-config --libs sdl2) -Llib -lcimgui"
|
LIBS="$(pkg-config --libs sdl2) -Llib -lcimgui"
|
||||||
SRC=src/*.c
|
SDL2_SDLRENDERER=src/sdl2-sdlrenderer.c
|
||||||
OUT=main
|
SDLRENDERER_OUT=sdl2-sdlrenderer
|
||||||
|
SDL2_OPENGL3=src/sdl2-opengl3.c
|
||||||
|
OPENGL3_OUT=sdl2-opengl3
|
||||||
|
|
||||||
(set -x ; $CC $CFLAGS $LIBS $SRC -o $OUT)
|
(set -x ; $CC $CFLAGS $LIBS $SDL2_SDLRENDERER -o $SDLRENDERER_OUT)
|
||||||
|
(set -x ; $CC $CFLAGS $LIBS -lGL -ldl $SDL2_OPENGL3 -o $OPENGL3_OUT)
|
||||||
|
BIN
lib/libcimgui.so
BIN
lib/libcimgui.so
Binary file not shown.
159
src/sdl2-opengl3.c
Normal file
159
src/sdl2-opengl3.c
Normal file
@ -0,0 +1,159 @@
|
|||||||
|
#include "SDL_rect.h"
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include <SDL2/SDL_events.h>
|
||||||
|
#include <SDL2/SDL_opengl.h>
|
||||||
|
#include <SDL2/SDL_render.h>
|
||||||
|
#include <SDL2/SDL_video.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#define CIMGUI_DEFINE_ENUMS_AND_STRUCTS
|
||||||
|
#include "cimgui/cimgui.h"
|
||||||
|
#define CIMGUI_USE_OPENGL3
|
||||||
|
#define CIMGUI_USE_SDL2
|
||||||
|
#include "cimgui/cimgui_impl.h"
|
||||||
|
#include <GL/gl.h>
|
||||||
|
#include <GL/glu.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
#define WINDOW_WIDTH 1280
|
||||||
|
#define WINDOW_HEIGHT 720
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
SDL_Init(SDL_INIT_EVERYTHING);
|
||||||
|
|
||||||
|
const char *glsl_version = "#version 130";
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
|
||||||
|
|
||||||
|
// Create window with graphics context
|
||||||
|
SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl");
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
|
||||||
|
SDL_DisplayMode current;
|
||||||
|
SDL_GetCurrentDisplayMode(0, ¤t);
|
||||||
|
|
||||||
|
SDL_WindowFlags window_flags =
|
||||||
|
(SDL_WindowFlags)(SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL |
|
||||||
|
SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
|
||||||
|
|
||||||
|
SDL_Window *window =
|
||||||
|
SDL_CreateWindow("Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
|
||||||
|
WINDOW_WIDTH, WINDOW_HEIGHT, window_flags);
|
||||||
|
|
||||||
|
SDL_GLContext gl_context = SDL_GL_CreateContext(window);
|
||||||
|
SDL_GL_MakeCurrent(window, gl_context);
|
||||||
|
SDL_GL_SetSwapInterval(1); // Enable vsync
|
||||||
|
|
||||||
|
igCreateContext(NULL);
|
||||||
|
ImGuiIO *io = igGetIO();
|
||||||
|
// Enable Keyboard Controls
|
||||||
|
io->ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
|
||||||
|
// Enable Gamepad Controls
|
||||||
|
io->ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
|
||||||
|
io->ConfigFlags |= ImGuiConfigFlags_DockingEnable;
|
||||||
|
io->ConfigFlags |= ImGuiConfigFlags_ViewportsEnable;
|
||||||
|
|
||||||
|
igStyleColorsDark(NULL);
|
||||||
|
|
||||||
|
ImGuiStyle *style = igGetStyle();
|
||||||
|
if (io->ConfigFlags & ImGuiConfigFlags_ViewportsEnable) {
|
||||||
|
style->WindowRounding = 0.0f;
|
||||||
|
style->Colors[ImGuiCol_WindowBg].w = 1.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!ImGui_ImplSDL2_InitForOpenGL(window, gl_context) ||
|
||||||
|
!ImGui_ImplOpenGL3_Init(glsl_version)) {
|
||||||
|
goto IMGUI_FAILED;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool running = true;
|
||||||
|
|
||||||
|
bool show_demo_window = true;
|
||||||
|
ImVec4 clear_color = {0.45f, 0.55f, 0.60f, 1.00f};
|
||||||
|
SDL_Event event = {0};
|
||||||
|
|
||||||
|
while (running) {
|
||||||
|
while (SDL_PollEvent(&event)) {
|
||||||
|
ImGui_ImplSDL2_ProcessEvent(&event);
|
||||||
|
switch (event.type) {
|
||||||
|
case SDL_QUIT:
|
||||||
|
running = false;
|
||||||
|
break;
|
||||||
|
case SDL_WINDOWEVENT:
|
||||||
|
if (event.window.event == SDL_WINDOWEVENT_CLOSE &&
|
||||||
|
event.window.windowID == SDL_GetWindowID(window)) {
|
||||||
|
running = false;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ImGui_ImplOpenGL3_NewFrame();
|
||||||
|
ImGui_ImplSDL2_NewFrame();
|
||||||
|
igNewFrame();
|
||||||
|
|
||||||
|
if (show_demo_window) {
|
||||||
|
igShowDemoWindow(&show_demo_window);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
static float f = 0.0f;
|
||||||
|
static int counter = 0;
|
||||||
|
|
||||||
|
igBegin("Hello, world!", NULL, 0);
|
||||||
|
|
||||||
|
igText("This is some useful text.");
|
||||||
|
|
||||||
|
igCheckbox("Demo Window", &show_demo_window);
|
||||||
|
|
||||||
|
igSliderFloat("float", &f, 0.0f, 1.0f, "0.3f", 0);
|
||||||
|
igColorEdit3("clear color", (float *)&clear_color, 0);
|
||||||
|
|
||||||
|
if (igButton("Button", (ImVec2){0, 0})) {
|
||||||
|
counter++;
|
||||||
|
}
|
||||||
|
|
||||||
|
igSameLine(0.0f, -1.0f);
|
||||||
|
igText("counter = %d", counter);
|
||||||
|
|
||||||
|
igText("Application average %.3f ms/frame (%.1f FPS)",
|
||||||
|
1000.0f / io->Framerate, io->Framerate);
|
||||||
|
igEnd();
|
||||||
|
}
|
||||||
|
|
||||||
|
igRender();
|
||||||
|
|
||||||
|
glViewport(0, 0, (int)io->DisplaySize.x, (int)io->DisplaySize.y);
|
||||||
|
glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w,
|
||||||
|
clear_color.z * clear_color.w, clear_color.w);
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
|
ImGui_ImplOpenGL3_RenderDrawData(igGetDrawData());
|
||||||
|
|
||||||
|
if (io->ConfigFlags & ImGuiConfigFlags_ViewportsEnable) {
|
||||||
|
SDL_Window *backup_current_window = SDL_GL_GetCurrentWindow();
|
||||||
|
SDL_GLContext backup_current_context = SDL_GL_GetCurrentContext();
|
||||||
|
igUpdatePlatformWindows();
|
||||||
|
igRenderPlatformWindowsDefault(NULL, NULL);
|
||||||
|
SDL_GL_MakeCurrent(backup_current_window, backup_current_context);
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_GL_SwapWindow(window);
|
||||||
|
}
|
||||||
|
|
||||||
|
ImGui_ImplOpenGL3_Shutdown();
|
||||||
|
ImGui_ImplSDL2_Shutdown();
|
||||||
|
igDestroyContext(NULL);
|
||||||
|
|
||||||
|
IMGUI_FAILED:
|
||||||
|
SDL_GL_DeleteContext(gl_context);
|
||||||
|
|
||||||
|
SDL_DestroyWindow(window);
|
||||||
|
|
||||||
|
SDL_Quit();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user