Skip to content
This repository has been archived by the owner on Oct 21, 2020. It is now read-only.

Commit

Permalink
Merge pull request #7 from gilmorem560/develop
Browse files Browse the repository at this point in the history
Fullscreen/windowed switching and glx-only mouse support
  • Loading branch information
Matthew Gilmore authored Apr 7, 2020
2 parents 4118eaf + 1c43a2a commit 7a0f1f2
Show file tree
Hide file tree
Showing 28 changed files with 1,722 additions and 420 deletions.
Binary file added data/textures/rgba_font.bin
Binary file not shown.
Binary file added data/textures/rgba_menu.bin
Binary file not shown.
15 changes: 13 additions & 2 deletions game-template.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<CompileAs>CompileAsC</CompileAs>
</ClCompile>
Expand Down Expand Up @@ -130,7 +130,7 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
Expand All @@ -142,6 +142,8 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="src\graphics\objects\hedrons.c" />
<ClCompile Include="src\graphics\objects\scene.c" />
<ClCompile Include="src\graphics\objects\texture.c" />
<ClCompile Include="src\graphics\primitives\grid2d.c">
<CompileAs Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Default</CompileAs>
</ClCompile>
Expand Down Expand Up @@ -172,11 +174,15 @@
<ClCompile Include="src\modes\diamond.c" />
<ClCompile Include="src\modes\map.c" />
<ClCompile Include="src\modes\sandbox.c" />
<ClCompile Include="src\modes\scene_test.c" />
<ClCompile Include="src\modes\stage.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\assets\assets.h" />
<ClInclude Include="src\graphics\contexts\glcontext.h" />
<ClInclude Include="src\graphics\objects\hedrons.h" />
<ClInclude Include="src\graphics\objects\scene.h" />
<ClInclude Include="src\graphics\objects\texture.h" />
<ClInclude Include="src\graphics\primitives\coord.h" />
<ClInclude Include="src\graphics\primitives\prim.h" />
<ClInclude Include="src\graphics\contexts\wglcontext.h" />
Expand All @@ -185,9 +191,14 @@
<ClInclude Include="src\modes\map.h" />
<ClInclude Include="src\modes\modes.h" />
<ClInclude Include="src\modes\sandbox.h" />
<ClInclude Include="src\modes\scene_test.h" />
<ClInclude Include="src\modes\stage.h" />
<ClInclude Include="src\util\macros.h" />
</ItemGroup>
<ItemGroup>
<None Include="data\textures\rgba_font.bin" />
<None Include="data\textures\rgba_menu.bin" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
Expand Down
35 changes: 35 additions & 0 deletions game-template.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@
<Filter Include="Source Files\obj\prim">
<UniqueIdentifier>{993e7676-aac2-45e4-8bd2-c5dc89c4c403}</UniqueIdentifier>
</Filter>
<Filter Include="Resource Files\data">
<UniqueIdentifier>{983f9f7b-a250-4813-a8fe-cbc2d23edb26}</UniqueIdentifier>
</Filter>
<Filter Include="Resource Files\data\textures">
<UniqueIdentifier>{a58543b1-e451-49f9-8eeb-58b7a45fa8d0}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\main.c">
Expand Down Expand Up @@ -84,6 +90,15 @@
<ClCompile Include="src\modes\stage.c">
<Filter>Source Files\mode</Filter>
</ClCompile>
<ClCompile Include="src\modes\scene_test.c">
<Filter>Source Files\mode</Filter>
</ClCompile>
<ClCompile Include="src\graphics\objects\scene.c">
<Filter>Source Files\obj</Filter>
</ClCompile>
<ClCompile Include="src\graphics\objects\texture.c">
<Filter>Source Files\obj</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\input\input.h">
Expand Down Expand Up @@ -122,5 +137,25 @@
<ClInclude Include="src\graphics\primitives\prim.h">
<Filter>Header Files\obj</Filter>
</ClInclude>
<ClInclude Include="src\assets\assets.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\modes\scene_test.h">
<Filter>Header Files\mode</Filter>
</ClInclude>
<ClInclude Include="src\graphics\objects\scene.h">
<Filter>Header Files\obj</Filter>
</ClInclude>
<ClInclude Include="src\graphics\objects\texture.h">
<Filter>Header Files\obj</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="data\textures\rgba_font.bin">
<Filter>Resource Files\data\textures</Filter>
</None>
<None Include="data\textures\rgba_menu.bin">
<Filter>Resource Files\data\textures</Filter>
</None>
</ItemGroup>
</Project>
8 changes: 7 additions & 1 deletion game-template.vcxproj.user
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LocalDebuggerCommandArguments>1</LocalDebuggerCommandArguments>
<LocalDebuggerCommandArguments>
</LocalDebuggerCommandArguments>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LocalDebuggerDebuggerType>Auto</LocalDebuggerDebuggerType>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
<LocalDebuggerCommandArguments>2&gt; &amp;1</LocalDebuggerCommandArguments>
</PropertyGroup>
</Project>
3 changes: 3 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ main_SOURCES = \
graphics/primitives/poly3d.c \
graphics/objects/hedrons.c \
graphics/objects/prisms.c \
graphics/objects/scene.c \
graphics/objects/texture.c \
input/glxevent.c \
modes/diamond.c \
modes/map.c \
modes/sandbox.c \
modes/scene_test.c \
modes/stage.c \
main.c
main_LDFLAGS = -lX11
7 changes: 7 additions & 0 deletions src/graphics/contexts/glcontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ extern "C" {
#else
#include "glxcontext.h"
#endif /* _WIN32 */

unsigned short xres;
unsigned short yres;
bool isfullscreen;

void setwindowed(unsigned short w_xres, unsigned short w_yres);
void setfullscreen(void);

#ifdef __cplusplus
};
Expand Down
156 changes: 144 additions & 12 deletions src/graphics/contexts/glxcontext.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
/*
* glxinit - initialize OpenGL for X11
*/
void glxinit(int xres, int yres)
void glxinit()
{
XSetWindowAttributes attrs;
XColor cursor_color = { 0, 0, 0, 0, 0, 0 };
Pixmap cursor_pixmap;
const char null_bitmap[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
int major, minor;
int config_count;
dpy = NULL;
vis = NULL;
isfullscreen = true;

/* open X display */
dpy = XOpenDisplay(NULL);
Expand All @@ -28,15 +32,12 @@ void glxinit(int xres, int yres)
exit(EXIT_FAILURE);
}

/* check that it is sufficient */
/*if (decimal(major, minor) < 1.3) {
fprintf(stderr, "glxinit: version 1.3 or greater required, version %f detected\n", decimal(major, minor));
XCloseDisplay(dpy);
exit(EXIT_FAILURE);
}*/

/* determine default screen */
screen_number = XDefaultScreen(dpy);

/* get resolution */
xres = XDisplayWidth(dpy, screen_number);
yres = XDisplayHeight(dpy, screen_number);

/* retrieve root window */
root = XRootWindow(dpy, screen_number);
Expand Down Expand Up @@ -85,28 +86,41 @@ void glxinit(int xres, int yres)
}

/* set input events */
attrs.event_mask = KeyPressMask | KeyReleaseMask | StructureNotifyMask;
attrs.event_mask = KeyPressMask | KeyReleaseMask | StructureNotifyMask | PointerMotionMask;
attrs.override_redirect = True;

/* create window */
win = XCreateWindow(
dpy /* Display *display */
,root /* Window parent */
,400 /* int x */
,400 /* int y */
,0 /* int x */
,0 /* int y */
,xres /* unsigned int width */
,yres /* unsigned int height */
,1 /* unsigned int border_width */
,CopyFromParent /* int depth */
,CopyFromParent /* unsigned int class */
,vis->visual /* Visual *visual */
,CWEventMask /* unsigned long valuemask */
,CWEventMask | CWOverrideRedirect /* unsigned long valuemask */
,&attrs); /* XSetWindowAttributes *attributes */

/* allow auto-repeat of keys */
XkbSetDetectableAutoRepeat(dpy, True, NULL);

/* create on-screen rendering area */
window = glXCreateWindow(dpy, *config, win, NULL);

/* create invisible cursor pixmap */
cursor_pixmap = XCreateBitmapFromData(dpy, win, null_bitmap, 8, 8);

/* create custom (invisible) cursor - modes must implement cursors */
cursor = XCreatePixmapCursor(dpy, cursor_pixmap, cursor_pixmap, &cursor_color, &cursor_color, 0, 0);

/* free pixmap now that we're done */
XFreePixmap(dpy, cursor_pixmap);

/* associate it */
XDefineCursor(dpy, win, cursor);

/* associate context to window */
if (!glXMakeCurrent(dpy, window, context)) {
Expand All @@ -125,6 +139,9 @@ void glxinit(int xres, int yres)

/* display window */
XMapWindow(dpy, win);

/* grab keyboard */
XGrabKeyboard(dpy, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);

return;
}
Expand All @@ -134,17 +151,32 @@ void glxinit(int xres, int yres)
*/
void glxfree(void)
{
XSetWindowAttributes attrs;
attrs.override_redirect = False;

/* ungrab keyboard */
XUngrabKeyboard(dpy, CurrentTime);

/* hide window */
XUnmapWindow(dpy, win);

/* dissociate context from window */
glXMakeCurrent(dpy, None, NULL);

/* return visible cursor */
XUndefineCursor(dpy, win);

/* destroy on-screen rendering area */
glXDestroyWindow(dpy, window);

/* need to return the window to the window manager to close properly */
XChangeWindowAttributes(dpy, win, CWOverrideRedirect, &attrs);

/* destroy window */
XDestroyWindow(dpy, win);

/* delete cursor */
XFreeCursor(dpy, cursor);

/* destroy context */
glXDestroyContext(dpy, context);
Expand All @@ -160,3 +192,103 @@ void glxfree(void)

return;
}

void setwindowed(unsigned short w_xres, unsigned short w_yres)
{
if (isfullscreen) {
XSetWindowAttributes attrs;
attrs.override_redirect = False;

/* ungrab keyboard */
XUngrabKeyboard(dpy, CurrentTime);

/* hide window */
XUnmapWindow(dpy, win);

/* dissociate context from window */
glXMakeCurrent(dpy, None, NULL);

/* destroy on-screen rendering area */
glXDestroyWindow(dpy, window);

/* return control to window manager */
XChangeWindowAttributes(dpy, win, CWOverrideRedirect, &attrs);

/* resize window */
XResizeWindow(dpy, win, w_xres, w_yres);

/* create on-screen rendering area */
window = glXCreateWindow(dpy, *config, win, NULL);

/* associate context to window */
if (!glXMakeCurrent(dpy, window, context)) {
fprintf(stderr, "glxinit: could not associate context with window\n");
glXDestroyWindow(dpy, window);
XDestroyWindow(dpy, win);
glXDestroyContext(dpy, context);
XFree(vis);
XFree(config);
XCloseDisplay(dpy);
exit(EXIT_FAILURE);
}

/* reshow window */
XMapWindow(dpy, win);

XGrabKeyboard(dpy, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);

/* mark windowed */
isfullscreen = false;
}

return;
}

void setfullscreen(void)
{
if (!isfullscreen) {
XSetWindowAttributes attrs;
attrs.override_redirect = True;

/* hide window */
XUnmapWindow(dpy, win);

/* dissociate context from window */
glXMakeCurrent(dpy, None, NULL);

/* destroy on-screen rendering area */
glXDestroyWindow(dpy, window);

/* take control from window manager */
XChangeWindowAttributes(dpy, win, CWOverrideRedirect, &attrs);

/* create on-screen rendering area */
window = glXCreateWindow(dpy, *config, win, NULL);

/* associate context to window */
if (!glXMakeCurrent(dpy, window, context)) {
fprintf(stderr, "glxinit: could not associate context with window\n");
glXDestroyWindow(dpy, window);
XDestroyWindow(dpy, win);
glXDestroyContext(dpy, context);
XFree(vis);
XFree(config);
XCloseDisplay(dpy);
exit(EXIT_FAILURE);
}

/* resize window */
XMoveResizeWindow(dpy, win, 0, 0, xres, yres);

/* unhide window */
XMapWindow(dpy, win);

/* grab keyboard */
XGrabKeyboard(dpy, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);

/* mark fullscreen */
isfullscreen = true;
}

return;
}
Loading

0 comments on commit 7a0f1f2

Please sign in to comment.