Reputation: 101
I'm currently working on a 3D renderer in C using SDL2 and OpenGL. I wanted to include a GUI into my project and at first tried to use Nuklear but it didn't suit my use case. The only one I can see that suits my needs is DearImgui using the C bindings.
I have downloaded the git repo and compiled cmgui into a .dll file and included it into my projects directory. I also compiled the cimgui_sdl.dll and included that in my project directory and linked them in my makefile like so:
build:
gcc -o renderer -Wall -std=c99 *.c \
-LC:/SDL2/lib/x64 -lSDL2main -lSDL2 -lopengl32 -lm \
-LC:/glew-2.1.0/lib/Release/x64 -lglew32 \
-LC:/cglm-master/build -lcglm-0 \
-LC:/Assimp/bin -lassimp-vc142-mtd \
-LC:/Users/Jamie/Documents/cimgui/cimgui -lcimgui \
-LC:/Users/Jamie/Documents/cimgui/backend_test/example_sdl_opengl3/build -lcimgui_sdl
If I compile my project this works fine and it all links correctly. My main issue now is I'm not sure what files to add to my include
folder in my project directory.
Here is my main.c
file:
#include <stdio.h>
#include <C:/SDL2/include/SDL2/SDL.h>
#include "include/GL/glew.h"
#include <stdbool.h>
#define CIMGUI_DEFINE_ENUMS_AND_STRUCTS
#include "include/cimgui/cimgui.h"
#include "include/cimgui/cimgui_impl_sdl.h"
#include "include/cimgui/cimgui_impl_opengl3.h"
// Window parameters
static int window_width = 640;
static int window_height = 480;
// Global variables
float time = 0;
bool is_running = false;
int previous_frame_time = 0;
float delta_time = 0;
#define FPS 60
#define FRAME_TARGET_TIME (1000 / FPS)
static SDL_Window* window = NULL;
SDL_GLContext context = NULL;
///////////////////////////////////////////////////////////////////////////////
// Setup function to initialize variables and game objects
///////////////////////////////////////////////////////////////////////////////
int setup(void) {
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
fprintf(stderr, "Error initializing SDL: %s\n", SDL_GetError());
return false;
}
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, 3);
// Create SDL Window
window = SDL_CreateWindow(
"The window into Jamie's madness",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
window_width, window_height,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
);
if (!window) {
fprintf(stderr, "Error creating SDL window: %s\n", SDL_GetError());
return false;
}
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_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
context = SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(window, context);
SDL_GL_SetSwapInterval(1); // Enable V-Sync
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Error initializing GLEW\n");
return false;
}
glViewport(0, 0, window_width, window_height);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
return true;
}
///////////////////////////////////////////////////////////////////////////////
// Process input
///////////////////////////////////////////////////////////////////////////////
void process_input(void) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
is_running = false;
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE) {
is_running = false;
}
break;
}
}
}
///////////////////////////////////////////////////////////////////////////////
// Update function frame by frame with a fixed time step
///////////////////////////////////////////////////////////////////////////////
void update(void) {
int current_time = SDL_GetTicks();
delta_time = (current_time - previous_frame_time) / 1000.0f;
previous_frame_time = current_time;
}
///////////////////////////////////////////////////////////////////////////////
// Render function to draw objects on the display
///////////////////////////////////////////////////////////////////////////////
void render(void) {
glClearColor(color[0], color[1], color[2], 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Swap buffers
SDL_GL_SwapWindow(window);
}
///////////////////////////////////////////////////////////////////////////////
// Free resources
///////////////////////////////////////////////////////////////////////////////
void free_resources(void) {
if (context) {
SDL_GL_DeleteContext(context);
}
if (window) {
SDL_DestroyWindow(window);
}
SDL_Quit();
}
///////////////////////////////////////////////////////////////////////////////
// Main function
///////////////////////////////////////////////////////////////////////////////
int main(int argc, char* args[]) {
setup();
is_running = true;
while (is_running) {
process_input();
update();
render();
}
free_resources();
return 0;
}
I then get errors saying it cant find "imgui.h" or some other .h
file. I've only a year of experience writing C and 0 C++ experience so Im not exactly sure what specific files I need to include to get this working as I've never tried to include this sort of C/C++ wrapper library. Any help would be greatly appreciated as I've not seen much online about trying to get this up and running.
-Edit- I've included
-I/C:\Users\Jamie\Documents\cimgui\imgui \
-I/C:/Users/Jamie/Documents/cimgui/imgui/backends \
in my MakeFile however, even though I've included the backend .cpp and .h files for sdl and opengl I'm still getting warning: implicit declaration of function INSERT_IMGUI_SDL_BACKEND_FUNCTION
Upvotes: 2
Views: 52