sgl

changeset 30:6242b03e7191

merged with win32 branch
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 28 Jun 2011 13:52:02 +0300
parents 171aa71b97e7 25de96fb1526
children 124195562f7e
files
diffstat 5 files changed, 691 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/sgl.sln	Tue Jun 28 13:52:02 2011 +0300
     1.3 @@ -0,0 +1,26 @@
     1.4 +
     1.5 +Microsoft Visual Studio Solution File, Format Version 11.00
     1.6 +# Visual Studio 2010
     1.7 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sgl", "sgl.vcxproj", "{5DA1BC2D-9C03-425E-BE3A-AAF75437045B}"
     1.8 +EndProject
     1.9 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "simple", "tests\simple\simple.vcxproj", "{440D0E79-9B82-4FD5-B8D6-D552938ED172}"
    1.10 +EndProject
    1.11 +Global
    1.12 +	GlobalSection(SolutionConfigurationPlatforms) = preSolution
    1.13 +		Debug|Win32 = Debug|Win32
    1.14 +		Release|Win32 = Release|Win32
    1.15 +	EndGlobalSection
    1.16 +	GlobalSection(ProjectConfigurationPlatforms) = postSolution
    1.17 +		{5DA1BC2D-9C03-425E-BE3A-AAF75437045B}.Debug|Win32.ActiveCfg = Debug|Win32
    1.18 +		{5DA1BC2D-9C03-425E-BE3A-AAF75437045B}.Debug|Win32.Build.0 = Debug|Win32
    1.19 +		{5DA1BC2D-9C03-425E-BE3A-AAF75437045B}.Release|Win32.ActiveCfg = Release|Win32
    1.20 +		{5DA1BC2D-9C03-425E-BE3A-AAF75437045B}.Release|Win32.Build.0 = Release|Win32
    1.21 +		{440D0E79-9B82-4FD5-B8D6-D552938ED172}.Debug|Win32.ActiveCfg = Debug|Win32
    1.22 +		{440D0E79-9B82-4FD5-B8D6-D552938ED172}.Debug|Win32.Build.0 = Debug|Win32
    1.23 +		{440D0E79-9B82-4FD5-B8D6-D552938ED172}.Release|Win32.ActiveCfg = Release|Win32
    1.24 +		{440D0E79-9B82-4FD5-B8D6-D552938ED172}.Release|Win32.Build.0 = Release|Win32
    1.25 +	EndGlobalSection
    1.26 +	GlobalSection(SolutionProperties) = preSolution
    1.27 +		HideSolutionNode = FALSE
    1.28 +	EndGlobalSection
    1.29 +EndGlobal
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/sgl.vcxproj	Tue Jun 28 13:52:02 2011 +0300
     2.3 @@ -0,0 +1,93 @@
     2.4 +<?xml version="1.0" encoding="utf-8"?>
     2.5 +<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
     2.6 +  <ItemGroup Label="ProjectConfigurations">
     2.7 +    <ProjectConfiguration Include="Debug|Win32">
     2.8 +      <Configuration>Debug</Configuration>
     2.9 +      <Platform>Win32</Platform>
    2.10 +    </ProjectConfiguration>
    2.11 +    <ProjectConfiguration Include="Release|Win32">
    2.12 +      <Configuration>Release</Configuration>
    2.13 +      <Platform>Win32</Platform>
    2.14 +    </ProjectConfiguration>
    2.15 +  </ItemGroup>
    2.16 +  <ItemGroup>
    2.17 +    <ClCompile Include="src\cb.c" />
    2.18 +    <ClCompile Include="src\log.c" />
    2.19 +    <ClCompile Include="src\modules.c" />
    2.20 +    <ClCompile Include="src\sgl.c" />
    2.21 +    <ClCompile Include="src\wsys.c" />
    2.22 +    <ClCompile Include="src\wsys_glut.c" />
    2.23 +    <ClCompile Include="src\wsys_sdl.c" />
    2.24 +    <ClCompile Include="src\wsys_w32.c" />
    2.25 +  </ItemGroup>
    2.26 +  <ItemGroup>
    2.27 +    <ClInclude Include="src\config.h" />
    2.28 +    <ClInclude Include="src\log.h" />
    2.29 +    <ClInclude Include="src\wsys.h" />
    2.30 +  </ItemGroup>
    2.31 +  <PropertyGroup Label="Globals">
    2.32 +    <ProjectGuid>{5DA1BC2D-9C03-425E-BE3A-AAF75437045B}</ProjectGuid>
    2.33 +    <Keyword>Win32Proj</Keyword>
    2.34 +    <RootNamespace>sgl</RootNamespace>
    2.35 +  </PropertyGroup>
    2.36 +  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
    2.37 +  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
    2.38 +    <ConfigurationType>StaticLibrary</ConfigurationType>
    2.39 +    <UseDebugLibraries>true</UseDebugLibraries>
    2.40 +    <CharacterSet>Unicode</CharacterSet>
    2.41 +  </PropertyGroup>
    2.42 +  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
    2.43 +    <ConfigurationType>StaticLibrary</ConfigurationType>
    2.44 +    <UseDebugLibraries>false</UseDebugLibraries>
    2.45 +    <WholeProgramOptimization>true</WholeProgramOptimization>
    2.46 +    <CharacterSet>Unicode</CharacterSet>
    2.47 +  </PropertyGroup>
    2.48 +  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
    2.49 +  <ImportGroup Label="ExtensionSettings">
    2.50 +  </ImportGroup>
    2.51 +  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
    2.52 +    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
    2.53 +  </ImportGroup>
    2.54 +  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
    2.55 +    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
    2.56 +  </ImportGroup>
    2.57 +  <PropertyGroup Label="UserMacros" />
    2.58 +  <PropertyGroup />
    2.59 +  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
    2.60 +    <ClCompile>
    2.61 +      <PrecompiledHeader>
    2.62 +      </PrecompiledHeader>
    2.63 +      <WarningLevel>Level3</WarningLevel>
    2.64 +      <Optimization>Disabled</Optimization>
    2.65 +      <PreprocessorDefinitions>WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
    2.66 +      <DisableSpecificWarnings>4996</DisableSpecificWarnings>
    2.67 +      <AdditionalIncludeDirectories>include</AdditionalIncludeDirectories>
    2.68 +    </ClCompile>
    2.69 +    <Link>
    2.70 +      <SubSystem>Windows</SubSystem>
    2.71 +      <GenerateDebugInformation>true</GenerateDebugInformation>
    2.72 +    </Link>
    2.73 +  </ItemDefinitionGroup>
    2.74 +  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
    2.75 +    <ClCompile>
    2.76 +      <WarningLevel>Level3</WarningLevel>
    2.77 +      <PrecompiledHeader>
    2.78 +      </PrecompiledHeader>
    2.79 +      <Optimization>MaxSpeed</Optimization>
    2.80 +      <FunctionLevelLinking>true</FunctionLevelLinking>
    2.81 +      <IntrinsicFunctions>true</IntrinsicFunctions>
    2.82 +      <PreprocessorDefinitions>WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
    2.83 +      <DisableSpecificWarnings>4996</DisableSpecificWarnings>
    2.84 +      <AdditionalIncludeDirectories>include</AdditionalIncludeDirectories>
    2.85 +    </ClCompile>
    2.86 +    <Link>
    2.87 +      <SubSystem>Windows</SubSystem>
    2.88 +      <GenerateDebugInformation>true</GenerateDebugInformation>
    2.89 +      <EnableCOMDATFolding>true</EnableCOMDATFolding>
    2.90 +      <OptimizeReferences>true</OptimizeReferences>
    2.91 +    </Link>
    2.92 +  </ItemDefinitionGroup>
    2.93 +  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
    2.94 +  <ImportGroup Label="ExtensionTargets">
    2.95 +  </ImportGroup>
    2.96 +</Project>
    2.97 \ No newline at end of file
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/wsys_w32.c	Tue Jun 28 13:52:02 2011 +0300
     3.3 @@ -0,0 +1,481 @@
     3.4 +/* SimplyGL window system module for Win32/WGL */
     3.5 +/* link-with: -lopengl32 */
     3.6 +
     3.7 +#include "config.h"
     3.8 +
     3.9 +#ifdef USE_WSYS_MODULE_W32
    3.10 +
    3.11 +#include <stdlib.h>
    3.12 +#include <assert.h>
    3.13 +#include <windows.h>
    3.14 +#include "sgl.h"
    3.15 +#include "wsys.h"
    3.16 +#include "log.h"
    3.17 +
    3.18 +struct window {
    3.19 +	int id;
    3.20 +	HWND win;
    3.21 +	HDC dc;
    3.22 +	HGLRC ctx;
    3.23 +	int width, height;
    3.24 +	int redisp_pending;
    3.25 +
    3.26 +	struct window *next;
    3.27 +};
    3.28 +
    3.29 +static int init(void);
    3.30 +static void shut_down(void);
    3.31 +
    3.32 +/* video mode switching */
    3.33 +static int set_vidmode(int xsz, int ysz);
    3.34 +static int get_vidmode(int *xsz, int *ysz);
    3.35 +
    3.36 +/* create/destroy windows */
    3.37 +static int create_window(int xsz, int ysz, unsigned int flags);
    3.38 +static void close_window(int id);
    3.39 +
    3.40 +/* window management */
    3.41 +static int set_active(int id);
    3.42 +static struct window *find_window(int id);
    3.43 +static struct window *find_window_handle(HWND wh);
    3.44 +static int activate_window(struct window *win);
    3.45 +static int set_title(const char *str);
    3.46 +static void redisplay(void);
    3.47 +static void swap_buffers(void);
    3.48 +
    3.49 +static int get_modifiers(void);
    3.50 +
    3.51 +/* event handling and friends */
    3.52 +static void set_bits(long *mask, long bits);
    3.53 +static void clear_bits(long *mask, long bits);
    3.54 +static void set_event(int idx, int enable);
    3.55 +static int process_events(void);
    3.56 +
    3.57 +static LRESULT CALLBACK handle_events(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);
    3.58 +static int mouse_button(unsigned int msg);
    3.59 +static void calc_win_size(int w, int h, int *width, int *height, int *posx, int *posy);
    3.60 +
    3.61 +
    3.62 +static struct wsys_module ws = {
    3.63 +	"win32", 0,
    3.64 +	init,
    3.65 +	shut_down,
    3.66 +	set_vidmode,
    3.67 +	get_vidmode,
    3.68 +	create_window,
    3.69 +	close_window,
    3.70 +	set_active,
    3.71 +	set_title,
    3.72 +	redisplay,
    3.73 +	swap_buffers,
    3.74 +	get_modifiers,
    3.75 +	set_event,
    3.76 +	process_events,
    3.77 +	0
    3.78 +};
    3.79 +
    3.80 +static struct window *winlist;
    3.81 +static struct window *active_win, *prev_active;
    3.82 +static int next_id = 1;
    3.83 +
    3.84 +/* this is the only exported function, everything else should be static */
    3.85 +void sgl_register_w32(void)
    3.86 +{
    3.87 +	sgl_register_module(&ws);
    3.88 +}
    3.89 +
    3.90 +
    3.91 +static int init(void)
    3.92 +{
    3.93 +	winlist = 0;
    3.94 +	active_win = prev_active = 0;
    3.95 +	return 0;
    3.96 +}
    3.97 +
    3.98 +static void shut_down(void)
    3.99 +{
   3.100 +	/* TODO */
   3.101 +}
   3.102 +
   3.103 +static int set_vidmode(int xsz, int ysz)
   3.104 +{
   3.105 +	DEVMODE dmode;
   3.106 +	memset(&dmode, 0, sizeof dmode);
   3.107 +	dmode.dmSize = sizeof dmode;
   3.108 +	/*dmode.dmBitsPerPel = 32;*/
   3.109 +	dmode.dmPelsWidth = xsz;
   3.110 +	dmode.dmPelsHeight = ysz;
   3.111 +	dmode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
   3.112 +
   3.113 +	if(ChangeDisplaySettings(&dmode, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL) {
   3.114 +		sgl_log("failed to set video mode: %dx%d\n", xsz, ysz);
   3.115 +		return -1;
   3.116 +	}
   3.117 +	return 0;
   3.118 +}
   3.119 +
   3.120 +static int get_vidmode(int *xsz, int *ysz)
   3.121 +{
   3.122 +	/* TODO */
   3.123 +	return 0;
   3.124 +}
   3.125 +
   3.126 +static int create_window(int xsz, int ysz, unsigned int flags)
   3.127 +{
   3.128 +	WNDCLASS wclass;
   3.129 +	int win_width, win_height, win_x, win_y;
   3.130 +	unsigned int style;
   3.131 +	HINSTANCE pid;
   3.132 +	HWND win;
   3.133 +	PIXELFORMATDESCRIPTOR fmt;
   3.134 +	int pf_id;
   3.135 +	struct window *wnode;
   3.136 +	void (*func)();
   3.137 +
   3.138 +	if(!(wnode = malloc(sizeof *wnode))) {
   3.139 +		return -1;
   3.140 +	}
   3.141 +
   3.142 +	pid = GetModuleHandle(0);
   3.143 +
   3.144 +	memset(&wclass, 0, sizeof wclass);
   3.145 +	wclass.style = CS_VREDRAW | CS_HREDRAW | CS_OWNDC;
   3.146 +	wclass.lpfnWndProc = handle_events;
   3.147 +	wclass.hInstance = pid;
   3.148 +	wclass.lpszClassName = "simplygl";
   3.149 +
   3.150 +	if(!RegisterClass(&wclass)) {
   3.151 +		sgl_log("failed to register window class\n");
   3.152 +		return -1;
   3.153 +	}
   3.154 +
   3.155 +	calc_win_size(xsz, ysz, &win_width, &win_height, &win_x, &win_y);
   3.156 +
   3.157 +	style = WS_BORDER | WS_CAPTION | WS_MINIMIZEBOX | WS_POPUP | WS_SYSMENU | WS_VISIBLE;
   3.158 +
   3.159 +	if(!(win = CreateWindow("simplygl", "OpenGL/Win32", style, win_x, win_y, win_width, win_height, 0, 0, pid, 0))) {
   3.160 +		sgl_log("failed to create window\n");
   3.161 +		return -1;
   3.162 +	}
   3.163 +
   3.164 +	ShowWindow(win, SW_SHOW);
   3.165 +	SetForegroundWindow(win);
   3.166 +	SetActiveWindow(win);
   3.167 +
   3.168 +	wnode->win = win;
   3.169 +	wnode->dc = GetDC(win);
   3.170 +
   3.171 +	memset(&fmt, 0, sizeof fmt);
   3.172 +	fmt.nSize = sizeof fmt;
   3.173 +	fmt.nVersion = 1;
   3.174 +	fmt.cColorBits = PFD_TYPE_RGBA;
   3.175 +	fmt.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
   3.176 +
   3.177 +	if(flags & SGL_DOUBLE) {
   3.178 +		fmt.dwFlags |= PFD_DOUBLEBUFFER;
   3.179 +	}
   3.180 +	if(flags & SGL_DEPTH) {
   3.181 +		fmt.cDepthBits = 24;
   3.182 +	}
   3.183 +	if(flags & SGL_STENCIL) {
   3.184 +		fmt.cStencilBits = 8;
   3.185 +	}
   3.186 +	/* TODO: more... */
   3.187 +
   3.188 +	if(!(pf_id = ChoosePixelFormat(wnode->dc, &fmt))) {
   3.189 +		sgl_log("can't find requested pixel format\n");
   3.190 +		return -1;
   3.191 +	}
   3.192 +	if(!SetPixelFormat(wnode->dc, pf_id, &fmt)) {
   3.193 +		sgl_log("failed to set pixel format\n");
   3.194 +		return -1;
   3.195 +	}
   3.196 +	if(!(wnode->ctx = wglCreateContext(wnode->dc))) {
   3.197 +		sgl_log("failed to create OpenGL context\n");
   3.198 +		return -1;
   3.199 +	}
   3.200 +
   3.201 +	wnode->id = next_id++;
   3.202 +	wnode->width = xsz;
   3.203 +	wnode->height = ysz;
   3.204 +	wnode->redisp_pending = 1;
   3.205 +	wnode->next = winlist;
   3.206 +	winlist = wnode;
   3.207 +
   3.208 +	if(!active_win) {
   3.209 +		set_active(wnode->id);
   3.210 +	} else {
   3.211 +		activate_window(wnode);
   3.212 +	}
   3.213 +
   3.214 +	if((func = sgl_get_callback(SGL_CREATE))) {
   3.215 +		func(wnode->id);
   3.216 +	}
   3.217 +	if((func = sgl_get_callback(SGL_RESHAPE))) {
   3.218 +		func(xsz, ysz);
   3.219 +	}
   3.220 +	activate_window(prev_active);
   3.221 +	return wnode->id;
   3.222 +}
   3.223 +
   3.224 +static void close_window(int id)
   3.225 +{
   3.226 +	struct window dummy, *win, *prev;
   3.227 +	sgl_close_callback_t close_func;
   3.228 +
   3.229 +	dummy.next = winlist;
   3.230 +
   3.231 +	prev = &dummy;
   3.232 +	win = prev->next;
   3.233 +
   3.234 +	while(win) {
   3.235 +		if(win->id == id) {
   3.236 +			if((close_func = sgl_get_callback(SGL_CLOSE))) {
   3.237 +				close_func(id);
   3.238 +			}
   3.239 +			wglDeleteContext(win->ctx);
   3.240 +			ReleaseDC(win->win, win->dc);
   3.241 +			DestroyWindow(win->win);
   3.242 +
   3.243 +			prev->next = win->next;
   3.244 +			free(win);
   3.245 +			return;
   3.246 +		}
   3.247 +		prev = win;
   3.248 +		win = win->next;
   3.249 +	}
   3.250 +}
   3.251 +
   3.252 +static int set_active(int id)
   3.253 +{
   3.254 +	struct window *win = find_window(id);
   3.255 +	if(!win) {
   3.256 +		sgl_log("no such window: %d\n", id);
   3.257 +		return -1;
   3.258 +	}
   3.259 +	/* only the user calls this, so don't revert this selection */
   3.260 +	prev_active = win;
   3.261 +	return activate_window(win);
   3.262 +}
   3.263 +
   3.264 +static struct window *find_window(int id)
   3.265 +{
   3.266 +	struct window *win = winlist;
   3.267 +
   3.268 +	while(win) {
   3.269 +		if(win->id == id) {
   3.270 +			return win;
   3.271 +		}
   3.272 +		win = win->next;
   3.273 +	}
   3.274 +	return 0;
   3.275 +}
   3.276 +
   3.277 +static struct window *find_window_handle(HWND wh)
   3.278 +{
   3.279 +	struct window *win = winlist;
   3.280 +
   3.281 +	while(win) {
   3.282 +		if(win->win == wh) {
   3.283 +			return win;
   3.284 +		}
   3.285 +		win = win->next;
   3.286 +	}
   3.287 +	return 0;
   3.288 +}
   3.289 +
   3.290 +static int activate_window(struct window *win)
   3.291 +{
   3.292 +	if(!wglMakeCurrent(win->dc, win->ctx)) {
   3.293 +		sgl_log("failed to activate window %d\n", (int)win->id);
   3.294 +		return -1;
   3.295 +	}
   3.296 +	active_win = win;
   3.297 +	return 0;
   3.298 +}
   3.299 +
   3.300 +static int set_title(const char *str)
   3.301 +{
   3.302 +	SetWindowText(active_win->win, str);
   3.303 +	return 0;
   3.304 +}
   3.305 +
   3.306 +static void redisplay(void)
   3.307 +{
   3.308 +	InvalidateRect(active_win->win, 0, 0);
   3.309 +}
   3.310 +
   3.311 +static void swap_buffers(void)
   3.312 +{
   3.313 +	SwapBuffers(active_win->dc);
   3.314 +}
   3.315 +
   3.316 +static int get_modifiers(void)
   3.317 +{
   3.318 +	return 0;	/* TODO */
   3.319 +}
   3.320 +
   3.321 +static void set_event(int idx, int enable)
   3.322 +{
   3.323 +	/* not needed */
   3.324 +}
   3.325 +
   3.326 +static int process_events(void)
   3.327 +{
   3.328 +	int ret = 0;
   3.329 +	MSG msg;
   3.330 +	sgl_idle_callback_t idle;
   3.331 +
   3.332 +	idle = sgl_get_callback(SGL_IDLE);
   3.333 +
   3.334 +	prev_active = active_win;
   3.335 +
   3.336 +	if(!idle) {
   3.337 +		if(!GetMessage(&msg, 0, 0, 0)) {
   3.338 +			ret = -1;
   3.339 +			goto end;
   3.340 +		}
   3.341 +	} else {
   3.342 +		if(!PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
   3.343 +			ret = 0;
   3.344 +			goto end;
   3.345 +		}
   3.346 +	}
   3.347 +
   3.348 +	do {
   3.349 +		if(msg.message == WM_QUIT) {
   3.350 +			ret = -1;
   3.351 +			break;
   3.352 +		}
   3.353 +		TranslateMessage(&msg);
   3.354 +		DispatchMessage(&msg);
   3.355 +	} while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE));
   3.356 +
   3.357 +end:
   3.358 +	if(ret != -1 && idle) {
   3.359 +		idle();
   3.360 +	}
   3.361 +	activate_window(prev_active);
   3.362 +	return ret;
   3.363 +}
   3.364 +
   3.365 +static LRESULT CALLBACK handle_events(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
   3.366 +{
   3.367 +	static int state;
   3.368 +	struct window *win;
   3.369 +	void (*func)();
   3.370 +
   3.371 +	if((win = find_window_handle(hwnd))) {
   3.372 +		activate_window(win);
   3.373 +	} else {
   3.374 +		return DefWindowProc(hwnd, msg, wparam, lparam);
   3.375 +	}
   3.376 +
   3.377 +	switch(msg) {
   3.378 +	case WM_PAINT:
   3.379 +		if((func = sgl_get_callback(SGL_DISPLAY))) {
   3.380 +			func();
   3.381 +		}
   3.382 +		ValidateRect(hwnd, 0);
   3.383 +		break;
   3.384 +
   3.385 +	case WM_KEYDOWN:
   3.386 +	case WM_KEYUP:
   3.387 +		if((func = sgl_get_callback(SGL_KEYBOARD))) {
   3.388 +			func(MapVirtualKey(wparam, 2), msg == WM_KEYDOWN);
   3.389 +		}
   3.390 +		break;
   3.391 +
   3.392 +	case WM_LBUTTONDOWN:
   3.393 +	case WM_RBUTTONDOWN:
   3.394 +	case WM_MBUTTONDOWN:
   3.395 +		if(1) {
   3.396 +			state++;
   3.397 +		} else {
   3.398 +	case WM_LBUTTONUP:
   3.399 +	case WM_RBUTTONUP:
   3.400 +	case WM_MBUTTONUP:
   3.401 +			state--;
   3.402 +		}
   3.403 +		assert(state >= 0);
   3.404 +		if((func = sgl_get_callback(SGL_MOUSE))) {
   3.405 +			func(mouse_button(msg), state, LOWORD(lparam), HIWORD(lparam));
   3.406 +		}
   3.407 +		break;
   3.408 +
   3.409 +	case WM_MOUSEMOVE:
   3.410 +		if(state) {
   3.411 +			func = sgl_get_callback(SGL_MOTION);
   3.412 +		} else {
   3.413 +			func = sgl_get_callback(SGL_PASSIVE);
   3.414 +		}
   3.415 +		if(func) {
   3.416 +			func(LOWORD(lparam), HIWORD(lparam));
   3.417 +		}
   3.418 +		break;
   3.419 +
   3.420 +	default:
   3.421 +		return DefWindowProc(hwnd, msg, wparam, lparam);
   3.422 +	}
   3.423 +	return 0;
   3.424 +}
   3.425 +
   3.426 +static int mouse_button(unsigned int msg)
   3.427 +{
   3.428 +	switch(msg) {
   3.429 +	case WM_LBUTTONDOWN:
   3.430 +	case WM_LBUTTONUP:
   3.431 +		return SGL_LEFT_BUTTON;
   3.432 +
   3.433 +	case WM_RBUTTONDOWN:
   3.434 +	case WM_RBUTTONUP:
   3.435 +		return SGL_RIGHT_BUTTON;
   3.436 +
   3.437 +	case WM_MBUTTONDOWN:
   3.438 +	case WM_MBUTTONUP:
   3.439 +		return SGL_MIDDLE_BUTTON;
   3.440 +
   3.441 +	default:
   3.442 +		break;
   3.443 +	}
   3.444 +	return -1;
   3.445 +}
   3.446 +
   3.447 +static void calc_win_size(int w, int h, int *width, int *height, int *posx, int *posy)
   3.448 +{
   3.449 +	RECT rect;
   3.450 +	DEVMODE dmode;
   3.451 +	unsigned int style = WS_BORDER | WS_CAPTION | WS_MINIMIZEBOX | WS_POPUP | WS_SYSMENU | WS_VISIBLE;
   3.452 +
   3.453 +	rect.left = rect.top = 0;
   3.454 +	rect.right = w;
   3.455 +	rect.bottom = h;
   3.456 +
   3.457 +	AdjustWindowRect(&rect, style, 0);
   3.458 +
   3.459 +	*width = rect.right - rect.left;
   3.460 +	*height = rect.bottom - rect.top;
   3.461 +
   3.462 +	*posx = *posy = 0;
   3.463 +
   3.464 +	memset(&dmode, 0, sizeof dmode);
   3.465 +	dmode.dmSize = sizeof dmode;
   3.466 +
   3.467 +	if(EnumDisplaySettings(0, ENUM_CURRENT_SETTINGS, &dmode)) {
   3.468 +		int x, y;
   3.469 +
   3.470 +		x = (dmode.dmPelsWidth - *width) / 2;
   3.471 +		if(x > 0) {
   3.472 +			*posx = x;
   3.473 +		}
   3.474 +
   3.475 +		y = (dmode.dmPelsHeight - *height) / 2;
   3.476 +		if(y > 0) {
   3.477 +			*posy = y;
   3.478 +		}
   3.479 +	}
   3.480 +}
   3.481 +
   3.482 +#else
   3.483 +int sgl_wsys_w32_silence_the_fucking_empty_file_warnings;
   3.484 +#endif
     4.1 --- a/tests/simple/simple.c	Tue Jun 28 13:50:41 2011 +0300
     4.2 +++ b/tests/simple/simple.c	Tue Jun 28 13:52:02 2011 +0300
     4.3 @@ -1,5 +1,8 @@
     4.4  #include <stdio.h>
     4.5  #include <ctype.h>
     4.6 +#ifdef WIN32
     4.7 +#include <windows.h>
     4.8 +#endif	/* WIN32 */
     4.9  #ifndef __APPLE__
    4.10  #include <GL/gl.h>
    4.11  #else
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/tests/simple/simple.vcxproj	Tue Jun 28 13:52:02 2011 +0300
     5.3 @@ -0,0 +1,88 @@
     5.4 +<?xml version="1.0" encoding="utf-8"?>
     5.5 +<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
     5.6 +  <ItemGroup Label="ProjectConfigurations">
     5.7 +    <ProjectConfiguration Include="Debug|Win32">
     5.8 +      <Configuration>Debug</Configuration>
     5.9 +      <Platform>Win32</Platform>
    5.10 +    </ProjectConfiguration>
    5.11 +    <ProjectConfiguration Include="Release|Win32">
    5.12 +      <Configuration>Release</Configuration>
    5.13 +      <Platform>Win32</Platform>
    5.14 +    </ProjectConfiguration>
    5.15 +  </ItemGroup>
    5.16 +  <PropertyGroup Label="Globals">
    5.17 +    <ProjectGuid>{440D0E79-9B82-4FD5-B8D6-D552938ED172}</ProjectGuid>
    5.18 +    <Keyword>Win32Proj</Keyword>
    5.19 +    <RootNamespace>simple</RootNamespace>
    5.20 +  </PropertyGroup>
    5.21 +  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
    5.22 +  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
    5.23 +    <ConfigurationType>Application</ConfigurationType>
    5.24 +    <UseDebugLibraries>true</UseDebugLibraries>
    5.25 +    <CharacterSet>Unicode</CharacterSet>
    5.26 +  </PropertyGroup>
    5.27 +  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
    5.28 +    <ConfigurationType>Application</ConfigurationType>
    5.29 +    <UseDebugLibraries>false</UseDebugLibraries>
    5.30 +    <WholeProgramOptimization>true</WholeProgramOptimization>
    5.31 +    <CharacterSet>Unicode</CharacterSet>
    5.32 +  </PropertyGroup>
    5.33 +  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
    5.34 +  <ImportGroup Label="ExtensionSettings">
    5.35 +  </ImportGroup>
    5.36 +  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
    5.37 +    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
    5.38 +  </ImportGroup>
    5.39 +  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
    5.40 +    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
    5.41 +  </ImportGroup>
    5.42 +  <PropertyGroup Label="UserMacros" />
    5.43 +  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
    5.44 +    <LinkIncremental>true</LinkIncremental>
    5.45 +  </PropertyGroup>
    5.46 +  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
    5.47 +    <LinkIncremental>false</LinkIncremental>
    5.48 +  </PropertyGroup>
    5.49 +  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
    5.50 +    <ClCompile>
    5.51 +      <PrecompiledHeader>
    5.52 +      </PrecompiledHeader>
    5.53 +      <WarningLevel>Level3</WarningLevel>
    5.54 +      <Optimization>Disabled</Optimization>
    5.55 +      <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
    5.56 +      <AdditionalIncludeDirectories>$(SolutionDir)\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
    5.57 +    </ClCompile>
    5.58 +    <Link>
    5.59 +      <SubSystem>Console</SubSystem>
    5.60 +      <GenerateDebugInformation>true</GenerateDebugInformation>
    5.61 +      <AdditionalLibraryDirectories>$(SolutionDir)\Debug</AdditionalLibraryDirectories>
    5.62 +      <AdditionalDependencies>kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;sgl.lib;opengl32.lib;%(AdditionalDependencies)</AdditionalDependencies>
    5.63 +    </Link>
    5.64 +  </ItemDefinitionGroup>
    5.65 +  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
    5.66 +    <ClCompile>
    5.67 +      <WarningLevel>Level3</WarningLevel>
    5.68 +      <PrecompiledHeader>
    5.69 +      </PrecompiledHeader>
    5.70 +      <Optimization>MaxSpeed</Optimization>
    5.71 +      <FunctionLevelLinking>true</FunctionLevelLinking>
    5.72 +      <IntrinsicFunctions>true</IntrinsicFunctions>
    5.73 +      <PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
    5.74 +      <AdditionalIncludeDirectories>..\..\sgl\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
    5.75 +    </ClCompile>
    5.76 +    <Link>
    5.77 +      <SubSystem>Console</SubSystem>
    5.78 +      <GenerateDebugInformation>true</GenerateDebugInformation>
    5.79 +      <EnableCOMDATFolding>true</EnableCOMDATFolding>
    5.80 +      <OptimizeReferences>true</OptimizeReferences>
    5.81 +      <AdditionalLibraryDirectories>$(SolutionDir)\Release</AdditionalLibraryDirectories>
    5.82 +      <AdditionalDependencies>kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;sgl.lib;opengl32.lib;%(AdditionalDependencies)</AdditionalDependencies>
    5.83 +    </Link>
    5.84 +  </ItemDefinitionGroup>
    5.85 +  <ItemGroup>
    5.86 +    <ClCompile Include="simple.c" />
    5.87 +  </ItemGroup>
    5.88 +  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
    5.89 +  <ImportGroup Label="ExtensionTargets">
    5.90 +  </ImportGroup>
    5.91 +</Project>
    5.92 \ No newline at end of file