Skip to content

Commit

Permalink
Replace GLUT with SDL2 in mdlviewer
Browse files Browse the repository at this point in the history
  • Loading branch information
SamVanheer committed Mar 15, 2022
1 parent ffe83b9 commit d7f408a
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 43 deletions.
8 changes: 4 additions & 4 deletions projects/vs2019/mdlviewer.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_CRT_SECURE_NO_WARNINGS;_CRT_NONSTDC_NO_WARNINGS;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>../../utils/common;../../common</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>../../utils/common;../../common;../../external</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>opengl32.lib;glu32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>opengl32.lib;glu32.lib;../../lib/public/SDL2.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
Expand All @@ -87,14 +87,14 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;_CRT_SECURE_NO_WARNINGS;_CRT_NONSTDC_NO_WARNINGS;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>../../utils/common;../../common</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>../../utils/common;../../common;../../external</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>opengl32.lib;glu32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>opengl32.lib;glu32.lib;../../lib/public/SDL2.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
Expand Down
193 changes: 154 additions & 39 deletions utils/mdlviewer/mdlviewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@
// updates:
// 1-4-98 fixed initialization

#include <chrono>
#include <thread>

#include <stdio.h>

#include <windows.h>

#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glut.h>

#define SDL_MAIN_HANDLED
#include <SDL2/SDL.h>

#include "mathlib.h"
#undef DotProduct
Expand All @@ -31,6 +36,9 @@ float g_lambert = 1.5;
float gldepthmin = 0;
float gldepthmax = 10.0;

SDL_Window* g_pWindow = nullptr;

SDL_GLContext g_GLContext = NULL;

/*
=============
Expand All @@ -53,9 +61,9 @@ void mdlviewer_display( )
tempmodel.SetBlending( 0, 0.0 );
tempmodel.SetBlending( 1, 0.0 );

static float prev;
float curr = GetTickCount( ) / 1000.0;
tempmodel.AdvanceFrame( curr - prev );
static long long prev;
auto curr = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
tempmodel.AdvanceFrame((curr - prev) / 1000.0);
prev = curr;

tempmodel.DrawModel( );
Expand Down Expand Up @@ -118,14 +126,14 @@ void pan(int x, int y)
transx += (x-ox)/500.;
transy -= (y-oy)/500.;
ox = x; oy = y;
glutPostRedisplay();
//glutPostRedisplay();
}

void zoom(int x, int y)
{
transz += (x-ox)/20.;
ox = x;
glutPostRedisplay();
//glutPostRedisplay();
}

void rotate(int x, int y)
Expand All @@ -137,7 +145,7 @@ void rotate(int x, int y)
if (roty > 360.) roty -= 360.;
else if (roty < -360.) roty += 360.;
ox = x; oy = y;
glutPostRedisplay();
//glutPostRedisplay();
}

void motion(int x, int y)
Expand All @@ -150,24 +158,28 @@ void motion(int x, int y)
zoom( x, y );
}

void mouse(int button, int state, int x, int y)
void mouse(const SDL_MouseButtonEvent& event)
{
if(state == GLUT_DOWN) {
switch(button) {
case GLUT_LEFT_BUTTON:
mot = PAN;
motion(ox = x, oy = y);
break;
case GLUT_RIGHT_BUTTON:
mot = ROT;
motion(ox = x, oy = y);
break;
case GLUT_MIDDLE_BUTTON:
break;
if (event.type == SDL_MOUSEBUTTONDOWN)
{
switch (event.button)
{
case SDL_BUTTON_LEFT:
mot = PAN;
motion(ox = event.x, oy = event.y);
break;
case SDL_BUTTON_RIGHT:
mot = ROT;
motion(ox = event.x, oy = event.y);
break;
case SDL_BUTTON_MIDDLE:
break;
}
}
else if (event.type == SDL_MOUSEBUTTONUP)
{
mot = 0;
}
} else if (state == GLUT_UP) {
mot = 0;
}
}

void help(void)
Expand Down Expand Up @@ -210,9 +222,9 @@ void display(void)
mdlviewer_display( );

glPopMatrix();
glutSwapBuffers();
SDL_GL_SwapWindow(g_pWindow);

glutPostRedisplay();
//glutPostRedisplay();
}

void reshape(int w, int h)
Expand Down Expand Up @@ -244,28 +256,131 @@ void key(unsigned char key, int x, int y)
default:
break;
}
glutPostRedisplay();
//glutPostRedisplay();
}

#define WIN_SIZE 512
#define FRAMERATE 60

int main(int argc, char** argv)
{
if (argc != 2)
{
printf("usage : %s <filename>\n", argv[0] );
printf("usage : %s <filename>\n", argv[0]);
exit(1);
}

glutInitWindowSize(512, 512);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE);
(void)glutCreateWindow(argv[0]);
init( argv[1] );
glutDisplayFunc(display);
glutKeyboardFunc(key);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
return 0;
if (SDL_Init(SDL_INIT_VIDEO))
{
printf("Couldn't initialize SDL2\n");
exit(EXIT_FAILURE);
}

g_pWindow = SDL_CreateWindow(argv[0], SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIN_SIZE, WIN_SIZE, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);

if (!g_pWindow)
{
printf("Failed to create SDL Window\n");
exit(EXIT_FAILURE);
}

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, 8);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, true);

g_GLContext = SDL_GL_CreateContext(g_pWindow);

SDL_GL_MakeCurrent(g_pWindow, g_GLContext);

init(argv[1]);

SDL_Event event;

bool bQuit = false;

auto prev = std::chrono::system_clock::now();

while (!bQuit)
{
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
{
bQuit = true;
break;
}

case SDL_WINDOWEVENT:
{
switch (event.window.event)
{
case SDL_WINDOWEVENT_RESIZED:
{
reshape(event.window.data1, event.window.data2);
break;
}

case SDL_WINDOWEVENT_CLOSE:
{
bQuit = true;
break;
}
}
break;
}

case SDL_KEYUP:
{
int x, y;
SDL_GetMouseState(&x, &y);
key(event.key.keysym.sym, x, y);
break;
}

case SDL_MOUSEMOTION:
{
motion(event.motion.x, event.motion.y);
break;
}

case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
{
mouse(event.button);
break;
}

default: break;
}
}

//Quit immediately.
if (bQuit)
break;

auto now = std::chrono::system_clock::now();

if (std::chrono::duration_cast<std::chrono::milliseconds>(now - prev).count() >= (1000 / FRAMERATE))
{
display();

prev = now;
}

std::this_thread::sleep_for(std::chrono::milliseconds(1));
}

SDL_GL_DeleteContext(g_GLContext);
g_GLContext = NULL;
SDL_DestroyWindow(g_pWindow);
g_pWindow = nullptr;

SDL_Quit();

return 0;
}

0 comments on commit d7f408a

Please sign in to comment.