mirror of
https://github.com/GayPizzaSpecifications/padlab.git
synced 2025-08-16 11:21:32 +00:00
Core profile renderer skeleton
This commit is contained in:
27
src/CMakeLists.txt
Normal file
27
src/CMakeLists.txt
Normal file
@ -0,0 +1,27 @@
|
||||
set(SOURCES
|
||||
maths.h
|
||||
draw.h
|
||||
draw_common.c
|
||||
$<$<NOT:$<OR:$<BOOL:${USE_OPENGL}>,$<BOOL:${USE_OPENGL_LEGACY}>>>:draw.c>
|
||||
$<$<BOOL:${USE_OPENGL}>:draw_opengl_core.c>
|
||||
$<$<BOOL:${USE_OPENGL_LEGACY}>:draw_opengl.c>
|
||||
stick.h
|
||||
stick.c
|
||||
analogue.c)
|
||||
|
||||
if (USE_OPENGL)
|
||||
include(GL3WHelper)
|
||||
add_gl3w(gl3w)
|
||||
endif()
|
||||
|
||||
add_executable(${TARGET} ${SOURCES})
|
||||
target_link_libraries(${TARGET}
|
||||
$<$<PLATFORM_ID:Windows>:SDL2::SDL2main>
|
||||
SDL2::SDL2
|
||||
$<$<OR:$<BOOL:${USE_OPENGL}>,$<BOOL:${USE_OPENGL_LEGACY}>>:OpenGL::GL>
|
||||
$<$<BOOL:${USE_OPENGL}>:gl3w>
|
||||
$<$<BOOL:${GNU}>:m>)
|
||||
target_compile_options(${TARGET} PRIVATE
|
||||
$<$<BOOL:${GNU}>:-Wall -Wextra -pedantic -Wno-unused-parameter>)
|
||||
target_compile_definitions(${TARGET} PRIVATE
|
||||
$<$<OR:$<BOOL:${USE_OPENGL}>,$<BOOL:${USE_OPENGL_LEGACY}>>:USE_OPENGL>)
|
11
src/draw.c
11
src/draw.c
@ -62,11 +62,6 @@ void DrawLine(int x1, int y1, int x2, int y2)
|
||||
SDL_RenderDrawLine(rend, x1, y1, x2, y2);
|
||||
}
|
||||
|
||||
void DrawCircle(int x, int y, int r)
|
||||
{
|
||||
DrawCircleSteps(x, y, r, (int)(sqrt((double)r) * 8.0));
|
||||
}
|
||||
|
||||
void DrawCircleSteps(int x, int y, int r, int steps)
|
||||
{
|
||||
double stepsz = (double)TAU / steps;
|
||||
@ -87,12 +82,6 @@ void DrawCircleSteps(int x, int y, int r, int steps)
|
||||
}
|
||||
}
|
||||
|
||||
void DrawArc(int x, int y, int r, int startAng, int endAng)
|
||||
{
|
||||
const int steps = (int)(sqrt((double)r) * (double)abs(endAng - startAng) / 360.0 * 8.0);
|
||||
DrawArcSteps(x, y, r, startAng, endAng, steps);
|
||||
}
|
||||
|
||||
void DrawArcSteps(int x, int y, int r, int startAng, int endAng, int steps)
|
||||
{
|
||||
const double fstart = (double)startAng * DEG2RAD;
|
||||
|
15
src/draw_common.c
Normal file
15
src/draw_common.c
Normal file
@ -0,0 +1,15 @@
|
||||
#include "draw.h"
|
||||
#include "maths.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
void DrawCircle(int x, int y, int r)
|
||||
{
|
||||
const int steps = (int)(sqrt((double)r) * 8.0);
|
||||
DrawCircleSteps(x, y, r, steps);
|
||||
}
|
||||
|
||||
void DrawArc(int x, int y, int r, int startAng, int endAng)
|
||||
{
|
||||
const int steps = (int)(sqrt((double)r) * (double)abs(endAng - startAng) / 360.0 * 8.0);
|
||||
DrawArcSteps(x, y, r, startAng, endAng, steps);
|
||||
}
|
@ -150,11 +150,6 @@ void DrawLine(int x1, int y1, int x2, int y2)
|
||||
glEnd();
|
||||
}
|
||||
|
||||
void DrawCircle(int x, int y, int r)
|
||||
{
|
||||
DrawCircleSteps(x, y, r, (int)(sqrt((double)r) * 8.0));
|
||||
}
|
||||
|
||||
void DrawCircleSteps(int x, int y, int r, int steps)
|
||||
{
|
||||
// Circles look better when offset negatively by half a pixel w/o MSAA
|
||||
@ -178,12 +173,6 @@ void DrawCircleSteps(int x, int y, int r, int steps)
|
||||
glEnd();
|
||||
}
|
||||
|
||||
void DrawArc(int x, int y, int r, int startAng, int endAng)
|
||||
{
|
||||
const int steps = (int)(sqrt((double)r) * (double)abs(endAng - startAng) / 360.0 * 8.0);
|
||||
DrawArcSteps(x, y, r, startAng, endAng, steps);
|
||||
}
|
||||
|
||||
void DrawArcSteps(int x, int y, int r, int startAng, int endAng, int steps)
|
||||
{
|
||||
// Arcs look better when offset negatively by half a pixel w/o MSAA
|
||||
|
152
src/draw_opengl_core.c
Normal file
152
src/draw_opengl_core.c
Normal file
@ -0,0 +1,152 @@
|
||||
#include "draw.h"
|
||||
#include "maths.h"
|
||||
#include <GL/gl3w.h>
|
||||
#include <SDL_video.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define OPENGL_VERSION_MAJOR 3
|
||||
#define OPENGL_VERSION_MINOR 3
|
||||
|
||||
static SDL_GLContext* ctx = NULL;
|
||||
static SDL_Window* window = NULL;
|
||||
static uint32_t colour = 0x00000000;
|
||||
static uint32_t clrColour = 0x00000000;
|
||||
static bool antialias = false;
|
||||
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wunused-function"
|
||||
static void GlErrorCb(
|
||||
GLenum source,
|
||||
GLenum type,
|
||||
GLuint id,
|
||||
GLenum severity,
|
||||
GLsizei length,
|
||||
const GLchar* message,
|
||||
const GLvoid* userParam)
|
||||
{
|
||||
if (severity != GL_DEBUG_SEVERITY_NOTIFICATION)
|
||||
printf("%s\n", message);
|
||||
}
|
||||
#pragma clang diagnostic pop
|
||||
|
||||
|
||||
void DrawWindowHints(void)
|
||||
{
|
||||
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); // Enable MSAA
|
||||
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 8); // 8x MSAA
|
||||
|
||||
// Legacy OpenGL profile
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, OPENGL_VERSION_MAJOR);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, OPENGL_VERSION_MINOR);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
||||
}
|
||||
|
||||
int InitDraw(SDL_Window* _window)
|
||||
{
|
||||
window = _window;
|
||||
ctx = SDL_GL_CreateContext(window);
|
||||
if (ctx == NULL || window == NULL || SDL_GL_MakeCurrent(window, ctx))
|
||||
{
|
||||
fprintf(stderr, "%s\n", SDL_GetError());
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Load Core profile extensions
|
||||
if (gl3wInit() != GL3W_OK)
|
||||
{
|
||||
fprintf(stderr, "Failed to init Core profile\n");
|
||||
return -1;
|
||||
}
|
||||
if (!gl3wIsSupported(OPENGL_VERSION_MAJOR, OPENGL_VERSION_MINOR))
|
||||
{
|
||||
fprintf(stderr, "OpenGL %d.%d unsupported\n", 3, 3);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Set debug callback
|
||||
#if !defined NDEBUG && !defined __APPLE__
|
||||
glDebugMessageCallback(GlErrorCb, nullptr);
|
||||
glEnable(GL_DEBUG_OUTPUT);
|
||||
#endif
|
||||
|
||||
SDL_GL_SetSwapInterval(1); // Enable vsync
|
||||
|
||||
// Detect if MSAA is available & active
|
||||
int res;
|
||||
if (SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &res) == 0 && res == 1)
|
||||
if (SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &res) == 0 && res > 0)
|
||||
antialias = true;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void QuitDraw(void)
|
||||
{
|
||||
SDL_GL_MakeCurrent(window, NULL);
|
||||
SDL_GL_DeleteContext(ctx);
|
||||
ctx = NULL;
|
||||
window = NULL;
|
||||
}
|
||||
|
||||
|
||||
size GetDrawSizeInPixels(void)
|
||||
{
|
||||
size out;
|
||||
SDL_GL_GetDrawableSize(SDL_GL_GetCurrentWindow(), &out.w, &out.h);
|
||||
return out;
|
||||
}
|
||||
|
||||
void SetDrawViewport(size size)
|
||||
{
|
||||
glViewport(0, 0, size.w, size.h);
|
||||
}
|
||||
|
||||
|
||||
void SetDrawColour(uint32_t c)
|
||||
{
|
||||
colour = c;
|
||||
}
|
||||
|
||||
void DrawClear(void)
|
||||
{
|
||||
if (clrColour != colour)
|
||||
{
|
||||
const float mul = 1.0f / 255.0f;
|
||||
glClearColor(
|
||||
(GLclampf)((colour & 0xFF000000) >> 24) * mul,
|
||||
(GLclampf)((colour & 0x00FF0000) >> 16) * mul,
|
||||
(GLclampf)((colour & 0x0000FF00) >> 8) * mul,
|
||||
(GLclampf)((colour & 0x000000FF)) * mul);
|
||||
clrColour = colour;
|
||||
}
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
}
|
||||
|
||||
void DrawPoint(int x, int y)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void DrawRect(int x, int y, int w, int h)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void DrawLine(int x1, int y1, int x2, int y2)
|
||||
{
|
||||
|
||||
}
|
||||
void DrawCircleSteps(int x, int y, int r, int steps)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void DrawArcSteps(int x, int y, int r, int startAng, int endAng, int steps)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void DrawPresent(void)
|
||||
{
|
||||
SDL_GL_SwapWindow(window);
|
||||
}
|
Reference in New Issue
Block a user