diff --git a/win32/dx9_renderer.c b/win32/dx9_renderer.c deleted file mode 100644 index 48c246c..0000000 --- a/win32/dx9_renderer.c +++ /dev/null @@ -1,1719 +0,0 @@ -/******************************************************************/ -/* DirectX 9 Renderer */ -/******************************************************************/ - -#include "model3.h" -#include -#include -#include - -#define EXTERNAL_SHADERS 0 - -static float min_z; -static float max_z; - - -#if EXTERNAL_SHADERS - -static LPCTSTR vs_filename = "vertex_shader.vs"; -static LPCTSTR ps_filename = "pixel_shader.ps"; -static LPCTSTR vs2d_filename = "vertex_shader_2d.vs"; -static LPCTSTR ps2d_filename = "pixel_shader_2d.ps"; - -#else - -#include "shaders.h" - -#endif - - -D3DXMATRIX d3d_matrix_stack_get_top(void); -void d3d_matrix_stack_init(void); - -typedef struct -{ - float x; - float y; - float width; - float height; -} VIEWPORT_PARAMS; - -typedef struct -{ - D3DXVECTOR4 sun_vector; - D3DXVECTOR4 sun_params; -} LIGHTING_PARAMS; - -#define MAX_VIEWPORTS 32 -#define MAX_LIGHTS 32 - -static int current_viewport; -static int current_light; -static int num_viewports; -static int num_lights; - -static VIEWPORT_PARAMS viewport_params[MAX_VIEWPORTS]; -static LIGHTING_PARAMS lighting_params[MAX_LIGHTS]; - - -#define MESH_STATIC 0 -#define MESH_DYNAMIC 1 - -typedef struct -{ - D3DMATRIX transform; - D3DMATRIX normal; - - int mesh_index; - int viewport; - int lighting; -} MESH; - -typedef struct -{ - int vb_index; - int num_vertices; - int vb_index_alpha; - int num_vertices_alpha; -} MESH_CACHE; - -#define DYNAMIC_VB_SIZE 180000 -#define DYNAMIC_VB_SIZE_ALPHA 180000 -#define STATIC_VB_SIZE 1800000 -#define STATIC_VB_SIZE_ALPHA 200000 - -#define NUM_DYNAMIC_MESHES 2048 -#define NUM_STATIC_MESHES 32768 - -#define DYNAMIC_MESH_BUFFER_SIZE 2048 -#define STATIC_MESH_BUFFER_SIZE 65536 - -typedef struct -{ - float x, y, z; - float u, v; - float nx, ny, nz; - float tx, ty, twidth, theight; - D3DCOLOR color, color2; -} VERTEX; - -D3DVERTEXELEMENT9 vertex_decl[] = -{ - {0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, - {0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0}, - {0, 20, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0}, - {0, 32, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1}, - {0, 48, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0}, - {0, 52, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 1}, - D3DDECL_END() -}; - - - -typedef struct -{ - float x, y, z, w; - float u, v; -} VERTEX_2D; - -const DWORD VERTEX2D_FVF = (D3DFVF_XYZRHW | D3DFVF_TEX1 ); - -D3DVERTEXELEMENT9 vertex_decl_2d[] = -{ - {0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, - {0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0}, - D3DDECL_END() -}; - - - -static LPDIRECT3D9 d3d; -static LPDIRECT3DDEVICE9 device; - -static D3DCAPS9 device_caps; - -static LPDIRECT3DTEXTURE9 texture; -static LPDIRECT3DVERTEXBUFFER9 dynamic_vb; -static LPDIRECT3DVERTEXBUFFER9 static_vb; -static LPDIRECT3DVERTEXBUFFER9 dynamic_vb_alpha; -static LPDIRECT3DVERTEXBUFFER9 static_vb_alpha; -static LPDIRECT3DVERTEXSHADER9 vshader; -static LPDIRECT3DPIXELSHADER9 pshader; -static LPDIRECT3DVERTEXDECLARATION9 vertex_declaration; - -static LPDIRECT3DTEXTURE9 texture_2d[4]; -static LPDIRECT3DTEXTURE9 palette_2d; -static LPDIRECT3DTEXTURE9 priority_2d[4]; -static LPDIRECT3DVERTEXBUFFER9 vb_2d; -static LPDIRECT3DVERTEXSHADER9 vshader_2d; -static LPDIRECT3DPIXELSHADER9 pshader_2d; -static LPDIRECT3DVERTEXDECLARATION9 vertex_declaration_2d; - -static LPD3DXMATRIXSTACK matrix_stack; - -static LPDIRECT3DTEXTURE9 lightgun_cursor; - -static LPD3DXFONT font; - -static D3DMATRIX world_matrix; -static D3DMATRIX view_matrix; -static D3DMATRIX projection_matrix; - -static D3DVIEWPORT9 viewport; - -static char num_bits[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 }; - -static HWND main_window; -static HFONT hfont; - -static UINT32 matrix_start; - -static MESH_CACHE *static_mesh_cache; -static MESH_CACHE *dynamic_mesh_cache; - -static int static_mesh_cache_top = 0; -static int dynamic_mesh_cache_top = 0; - -static int dynamic_mesh_cache_vertex_top = 0; -static int dynamic_mesh_cache_vertex_top_alpha = 0; -static int static_mesh_cache_vertex_top = 0; -static int static_mesh_cache_vertex_top_alpha = 0; - -static MESH *static_mesh_buffer; -static int static_mesh_buffer_top = 0; -static MESH *dynamic_mesh_buffer; -static int dynamic_mesh_buffer_top = 0; - -static int listdone = 0; - -static D3DXVECTOR4 sun_vector; -static D3DXVECTOR4 sun_params; - -static BOOL traverse_node(UINT32); -static BOOL traverse_list(UINT32); -static BOOL render_scene(void); -static UINT32* get_address(UINT32); - - -static UINT16 *static_mesh_index; - - -static void d3d_shutdown(void) -{ - if(d3d) { - IDirect3D9_Release(d3d); - d3d = NULL; - } -} - -BOOL d3d_pre_init(void) -{ - HRESULT hr; - D3DDISPLAYMODE d3ddm; - D3DADAPTER_IDENTIFIER9 adapter_identifier; - - d3d = Direct3DCreate9(D3D_SDK_VERSION); - if (!d3d) - { - message(0, "Direct3DCreate9 failed.\n"); - return FALSE; - } - - hr = IDirect3D9_GetAdapterDisplayMode(d3d, D3DADAPTER_DEFAULT, &d3ddm); - if (FAILED(hr)) - { - message(0, "d3d->GetAdapterDisplayMode failed.\n"); - return FALSE; - } - - hr = IDirect3D9_GetDeviceCaps(d3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &device_caps); - if (FAILED(hr)) - { - message(0, "d3d->GetDeviceCaps failed.\n"); - return FALSE; - } - - hr = IDirect3D9_GetAdapterIdentifier(d3d, D3DADAPTER_DEFAULT, 0, &adapter_identifier); - if (FAILED(hr)) - { - message(0, "d3d->GetAdapterIdentifier failed.\n"); - return FALSE; - } - - message(0, "Video card: %s", adapter_identifier.Description); - - if (device_caps.VertexShaderVersion < D3DVS_VERSION(3,0)) - { - message(0, "The video card doesn't support Vertex Shader 3.0\n"); - return FALSE; - } - if (device_caps.PixelShaderVersion < D3DPS_VERSION(3,0)) - { - message(0, "The video card doesn't support Pixel Shader 3.0\n"); - return FALSE; - } - - return TRUE; -} - -BOOL d3d_init(HWND main_window) -{ - D3DPRESENT_PARAMETERS d3dpp; - LPD3DXBUFFER vsh, psh, errors; - HRESULT hr; - DWORD flags = 0; - int width, height; - int i; - - if (m3_config.fullscreen) - { - width = m3_config.width; - height = m3_config.height; - } - else - { - width = 496; - height = 384; - } - - // Check if we have a valid display mode - if (width == 0 || height == 0) - return FALSE; - - memset(&d3dpp, 0, sizeof(d3dpp)); - d3dpp.Windowed = m3_config.fullscreen ? FALSE : TRUE; - d3dpp.SwapEffect = m3_config.fullscreen ? (m3_config.stretch ? D3DSWAPEFFECT_COPY : D3DSWAPEFFECT_FLIP) : D3DSWAPEFFECT_FLIP; - d3dpp.BackBufferWidth = width; - d3dpp.BackBufferHeight = height; - d3dpp.BackBufferCount = m3_config.stretch ? 1 : 2; - d3dpp.hDeviceWindow = main_window; - d3dpp.PresentationInterval = m3_config.fullscreen ? D3DPRESENT_INTERVAL_ONE : D3DPRESENT_INTERVAL_IMMEDIATE; - d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8; - d3dpp.EnableAutoDepthStencil = TRUE; - d3dpp.AutoDepthStencilFormat = D3DFMT_D24X8; - - if (device_caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) - { - flags |= D3DCREATE_HARDWARE_VERTEXPROCESSING; - } - else - { - flags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING; - } - - hr = IDirect3D9_CreateDevice(d3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, main_window, flags, &d3dpp, &device); - if(FAILED(hr)) - { - return FALSE; - } - - // create vertex buffers for dynamic vertex data - hr = IDirect3DDevice9_CreateVertexBuffer(device, - DYNAMIC_VB_SIZE * sizeof(VERTEX), D3DUSAGE_DYNAMIC|D3DUSAGE_WRITEONLY, 0, D3DPOOL_DEFAULT, &dynamic_vb, NULL); - if (FAILED(hr)) - { - message(0, "d3d->CreateVertexBuffer failed\n"); - return FALSE; - } - hr = IDirect3DDevice9_CreateVertexBuffer(device, - DYNAMIC_VB_SIZE_ALPHA * sizeof(VERTEX), D3DUSAGE_DYNAMIC|D3DUSAGE_WRITEONLY, 0, D3DPOOL_DEFAULT, &dynamic_vb_alpha, NULL); - if (FAILED(hr)) - { - message(0, "d3d->CreateVertexBuffer failed\n"); - return FALSE; - } - - // create vertex buffers for static vertex data - hr = IDirect3DDevice9_CreateVertexBuffer(device, - STATIC_VB_SIZE * sizeof(VERTEX), 0, 0, D3DPOOL_MANAGED, &static_vb, NULL); - if (FAILED(hr)) - { - message(0, "d3d->CreateVertexBuffer failed\n"); - return FALSE; - } - hr = IDirect3DDevice9_CreateVertexBuffer(device, - STATIC_VB_SIZE_ALPHA * sizeof(VERTEX), 0, 0, D3DPOOL_MANAGED, &static_vb_alpha, NULL); - if (FAILED(hr)) - { - message(0, "d3d->CreateVertexBuffer failed\n"); - return FALSE; - } - -#if EXTERNAL_SHADERS - // create vertex shader - hr = D3DXAssembleShaderFromFile(vs_filename, NULL, NULL, 0, &vsh, &errors); - if (FAILED(hr)) - { - message(0, "Direct3D error: %s", errors->lpVtbl->GetBufferPointer(errors)); - return FALSE; - } - hr = IDirect3DDevice9_CreateVertexShader(device, (DWORD*)vsh->lpVtbl->GetBufferPointer(vsh), &vshader); - if (FAILED(hr)) - { - message(0, "Direct3D error: IDirect3DDevice9_CreateVertexShader failed."); - return FALSE; - } - - // create pixel shader - hr = D3DXAssembleShaderFromFile(ps_filename, NULL, NULL, 0, &psh, &errors); - if (FAILED(hr)) - { - message(0, "Direct3D error: %s", errors->lpVtbl->GetBufferPointer(errors)); - return FALSE; - } - hr = IDirect3DDevice9_CreatePixelShader(device, (DWORD*)psh->lpVtbl->GetBufferPointer(psh), &pshader); - if (FAILED(hr)) - { - message(0, "Direct3D error: IDirect3DDevice9_CreatePixelShader failed."); - return FALSE; - } -#else - // create vertex shader - hr = IDirect3DDevice9_CreateVertexShader(device, (DWORD*)vertex_shader_source, &vshader); - if (FAILED(hr)) - { - message(0, "Direct3D error: IDirect3DDevice9_CreateVertexShader failed."); - return FALSE; - } - - // create pixel shader - hr = IDirect3DDevice9_CreatePixelShader(device, (DWORD*)pixel_shader_source, &pshader); - if (FAILED(hr)) - { - message(0, "Direct3D error: IDirect3DDevice9_CreatePixelShader failed."); - return FALSE; - } -#endif - - // create vertex declarations - hr = IDirect3DDevice9_CreateVertexDeclaration(device, vertex_decl, &vertex_declaration); - if (FAILED(hr)) - { - message(0, "IDirect3DDevice9_CreateVertexDeclaration failed."); - return FALSE; - } - - // create textures - hr = IDirect3DDevice9_CreateTexture(device, 2048, 4096, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &texture, NULL); - if (FAILED(hr)) - { - message(0, "IDirect3DDevice9_CreateTexture failed."); - return FALSE; - } - - dynamic_mesh_buffer = (MESH*)malloc(sizeof(MESH) * DYNAMIC_MESH_BUFFER_SIZE); - static_mesh_buffer = (MESH*)malloc(sizeof(MESH) * STATIC_MESH_BUFFER_SIZE); - - dynamic_mesh_cache = (MESH_CACHE*)malloc(sizeof(MESH_CACHE) * NUM_DYNAMIC_MESHES); - static_mesh_cache = (MESH_CACHE*)malloc(sizeof(MESH_CACHE) * NUM_STATIC_MESHES); - - static_mesh_index = (UINT16*)malloc(sizeof(UINT16) * 16777216); - for (i=0; i < 16777216; i++) - { - static_mesh_index[i] = 0xffff; - } - - { - ///////////////////////////// - // 2D layers // - ///////////////////////////// - - VERTEX_2D *vb; - - // create vertex shader and pixel shader for 2D -#if EXTERNAL_SHADERS - // create vertex shader - hr = D3DXAssembleShaderFromFile(vs2d_filename, NULL, NULL, 0, &vsh, &errors); - if (FAILED(hr)) - { - message(0, "Direct3D error: %s", errors->lpVtbl->GetBufferPointer(errors)); - return FALSE; - } - hr = IDirect3DDevice9_CreateVertexShader(device, (DWORD*)vsh->lpVtbl->GetBufferPointer(vsh), &vshader_2d); - if (FAILED(hr)) - { - message(0, "Direct3D error: IDirect3DDevice9_CreateVertexShader failed."); - return FALSE; - } - - // create pixel shader - hr = D3DXAssembleShaderFromFile(ps2d_filename, NULL, NULL, 0, &psh, &errors); - if (FAILED(hr)) - { - message(0, "Direct3D error: %s", errors->lpVtbl->GetBufferPointer(errors)); - return FALSE; - } - hr = IDirect3DDevice9_CreatePixelShader(device, (DWORD*)psh->lpVtbl->GetBufferPointer(psh), &pshader_2d); - if (FAILED(hr)) - { - message(0, "Direct3D error: IDirect3DDevice9_CreatePixelShader failed."); - return FALSE; - } -#else - // create vertex shader - hr = IDirect3DDevice9_CreateVertexShader(device, (DWORD*)vertex_shader_2d_source, &vshader_2d); - if (FAILED(hr)) - { - message(0, "Direct3D error: IDirect3DDevice9_CreateVertexShader failed."); - return FALSE; - } - - // create pixel shader - hr = IDirect3DDevice9_CreatePixelShader(device, (DWORD*)pixel_shader_2d_source, &pshader_2d); - if (FAILED(hr)) - { - message(0, "Direct3D error: IDirect3DDevice9_CreatePixelShader failed."); - return FALSE; - } -#endif - - // create vertex declarations - hr = IDirect3DDevice9_CreateVertexDeclaration(device, vertex_decl, &vertex_declaration_2d); - if (FAILED(hr)) - { - message(0, "IDirect3DDevice9_CreateVertexDeclaration failed."); - return FALSE; - } - - // create textures for 2d layers - for (i=0; i < 4; i++) - { - hr = IDirect3DDevice9_CreateTexture(device, - 512, 512, 1, D3DUSAGE_DYNAMIC, D3DFMT_A8L8, D3DPOOL_DEFAULT, &texture_2d[i], NULL); - if (FAILED(hr)) - { - message(0, "IDirect3DDevice9_CreateTexture failed."); - return FALSE; - } - - // priority - hr = IDirect3DDevice9_CreateTexture(device, - 1, 512, 1, D3DUSAGE_DYNAMIC, D3DFMT_L8, D3DPOOL_DEFAULT, &priority_2d[i], NULL); - if (FAILED(hr)) - { - message(0, "IDirect3DDevice9_CreateTexture failed."); - return FALSE; - } - } - - // create texture for palette - hr = IDirect3DDevice9_CreateTexture(device, - 256, 256, 1, D3DUSAGE_DYNAMIC, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &palette_2d, NULL); - if (FAILED(hr)) - { - message(0, "IDirect3DDevice9_CreateTexture failed."); - return FALSE; - } - - - - // create vertex buffer for 2d layers - hr = IDirect3DDevice9_CreateVertexBuffer(device, 4 * sizeof(VERTEX_2D), 0, 0, D3DPOOL_MANAGED, &vb_2d, NULL); - if (FAILED(hr)) - { - message(0, "d3d->CreateVertexBuffer failed\n"); - return FALSE; - } - - // fill in the vertex data - IDirect3DVertexBuffer9_Lock(vb_2d, 0, 0, (void **)&vb, D3DLOCK_DISCARD); - - vb[0].x = 0.0f; vb[0].y = 0.0f; vb[0].z = 1.0f; vb[0].w = 1.0f; - vb[0].u = 0.0f; vb[0].v = 0.0f; - vb[1].x = 512.0f; vb[1].y = 0.0f; vb[1].z = 1.0f; vb[1].w = 1.0f; - vb[1].u = 1.0f; vb[1].v = 0.0f; - vb[2].x = 512.0f; vb[2].y = 512.0f; vb[2].z = 1.0f; vb[2].w = 1.0f; - vb[2].u = 1.0f; vb[2].v = 1.0f; - vb[3].x = 0.0f; vb[3].y = 512.0f; vb[3].z = 1.0f; vb[3].w = 1.0f; - vb[3].u = 0.0f; vb[3].v = 1.0f; - - IDirect3DVertexBuffer9_Unlock(vb_2d); - } - - D3DXCreateMatrixStack(0, &matrix_stack); - d3d_matrix_stack_init(); - - - // Create font for the on-screen display - hfont = CreateFont( 14, 0, // Width, Height - 0, 0, // Escapement, Orientation - FW_BOLD, // Font weight - FALSE, FALSE, // Italic, underline - FALSE, // Strikeout - ANSI_CHARSET, // Charset - OUT_DEFAULT_PRECIS, // Precision - CLIP_DEFAULT_PRECIS, // Clip precision - DEFAULT_QUALITY, // Output quality - DEFAULT_PITCH | // Pitch and family - FF_DONTCARE, - "Terminal" ); - if (hfont == NULL) - { - message(0, "Direct3D error: Couldn't create font."); - return FALSE; - } - - // Create D3D font - hr = D3DXCreateFont(device, 14, 0, FW_BOLD, 1, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, - DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Terminal", &font); - if (FAILED(hr)) - { - message(0, "Direct3D error: D3DXCreateFont failed."); - return FALSE; - } - - atexit(d3d_shutdown); - - // set minimum and maximum Z - if (m3_config.step == 0x10) - { - min_z = 10.0f; - max_z = 500000.0f; - } - else - { - min_z = 0.1f; - max_z = 100000.0f; - } - - return TRUE; -} - -static void set_viewport(int x, int y, int width, int height) -{ - D3DVIEWPORT9 viewport; - - memset(&viewport, 0, sizeof(D3DVIEWPORT9)); - viewport.X = x; - viewport.Y = y; - viewport.Width = width; - viewport.Height = height; - viewport.MinZ = min_z; - viewport.MaxZ = max_z; - - IDirect3DDevice9_SetViewport(device, &viewport); -} - -void osd_renderer_draw_layer(int layer, UINT32 color_offset, int x, int y, BOOL top) -{ - int i; - D3DXVECTOR4 scroll_pos; - float co[4]; - co[0] = 0.0f; - co[1] = (((float)((color_offset >> 16) & 0xff) / 255.0f) - 0.5f) * 2.0f; - co[2] = (((float)((color_offset >> 8) & 0xff) / 255.0f) - 0.5f) * 2.0f; - co[3] = (((float)((color_offset >> 0) & 0xff) / 255.0f) - 0.5f) * 2.0f; - - scroll_pos.x = (float)(x) / 512.0f; - scroll_pos.y = (float)(y) / 512.0f; - scroll_pos.z = top ? 1.0f : 0.0f; - - set_viewport(0, 0, 496, 384); - - IDirect3DDevice9_BeginScene(device); - - IDirect3DDevice9_SetStreamSourceFreq(device, 0, 1); - IDirect3DDevice9_SetStreamSource(device, 0, vb_2d, 0, sizeof(VERTEX_2D)); - IDirect3DDevice9_SetStreamSourceFreq(device, 1, 1); - IDirect3DDevice9_SetStreamSource(device, 1, NULL, 0, NULL); - - IDirect3DDevice9_SetFVF(device, VERTEX2D_FVF); -// IDirect3DDevice9_SetVertexDeclaration(device, vertex_declaration_2d); - IDirect3DDevice9_SetVertexShader(device, vshader_2d); - IDirect3DDevice9_SetPixelShader(device, pshader_2d); - - // set color offset - IDirect3DDevice9_SetPixelShaderConstantF(device, 8, (float *)co, 1); - IDirect3DDevice9_SetPixelShaderConstantF(device, 9, (float *)&scroll_pos, 1); - - IDirect3DDevice9_SetRenderState(device, D3DRS_CULLMODE, D3DCULL_NONE); - IDirect3DDevice9_SetRenderState(device, D3DRS_ZENABLE, D3DZB_FALSE); - IDirect3DDevice9_SetRenderState(device, D3DRS_FILLMODE, D3DFILL_SOLID); - - IDirect3DDevice9_SetRenderState(device, D3DRS_ALPHATESTENABLE, TRUE); - IDirect3DDevice9_SetRenderState(device, D3DRS_ALPHAREF, (DWORD)0x00000000); - IDirect3DDevice9_SetRenderState(device, D3DRS_ALPHAFUNC, D3DCMP_GREATER); - - IDirect3DDevice9_SetSamplerState(device, 0, D3DSAMP_MAGFILTER, D3DTEXF_POINT); - IDirect3DDevice9_SetSamplerState(device, 0, D3DSAMP_MINFILTER, D3DTEXF_POINT); - IDirect3DDevice9_SetSamplerState(device, 0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP); - IDirect3DDevice9_SetSamplerState(device, 0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP); - IDirect3DDevice9_SetSamplerState(device, 1, D3DSAMP_MINFILTER, D3DTEXF_NONE); - IDirect3DDevice9_SetSamplerState(device, 1, D3DSAMP_MAGFILTER, D3DTEXF_NONE); - IDirect3DDevice9_SetSamplerState(device, 1, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP); - IDirect3DDevice9_SetSamplerState(device, 1, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP); - IDirect3DDevice9_SetSamplerState(device, 2, D3DSAMP_MINFILTER, D3DTEXF_NONE); - IDirect3DDevice9_SetSamplerState(device, 2, D3DSAMP_MAGFILTER, D3DTEXF_NONE); - IDirect3DDevice9_SetSamplerState(device, 2, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP); - IDirect3DDevice9_SetSamplerState(device, 2, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP); - - IDirect3DDevice9_SetTexture(device, 0, texture_2d[layer]); - IDirect3DDevice9_SetTexture(device, 1, palette_2d); - IDirect3DDevice9_SetTexture(device, 2, priority_2d[layer]); - - IDirect3DDevice9_DrawPrimitive(device, D3DPT_TRIANGLEFAN, 0, 2); - - IDirect3DDevice9_EndScene(device); -} - -void osd_renderer_blit(void) -{ - if (m3_config.stretch && m3_config.fullscreen) - { - RECT src_rect = {0, 0, 496, 384}; - IDirect3DDevice9_Present(device, &src_rect, NULL, NULL, NULL); - } - else - { - IDirect3DDevice9_Present(device, NULL, NULL, NULL, NULL); - } -} - -void osd_renderer_clear(BOOL fbuf, BOOL zbuf) -{ - int flags = 0; - - set_viewport(0, 0, m3_config.width, m3_config.height); - - if (fbuf) - { - flags |= D3DCLEAR_TARGET; - } - if (zbuf) - { - flags |= D3DCLEAR_ZBUFFER; - } - - IDirect3DDevice9_Clear(device, 0, NULL, flags, 0x00000000, 1.0f, 0); -} - -void osd_renderer_get_layer_buffer(int layer_num, UINT8 **buffer, int *pitch) -{ - D3DLOCKED_RECT locked_rect; - HRESULT hr; - - *buffer = NULL; - *pitch = 0; - - hr = IDirect3DTexture9_LockRect(texture_2d[layer_num], 0, &locked_rect, NULL, D3DLOCK_DISCARD); - if (!FAILED(hr)) - { - *buffer = (UINT8 *)locked_rect.pBits; - *pitch = locked_rect.Pitch / 2; - } -} - -void osd_renderer_free_layer_buffer(UINT layer_num) -{ - IDirect3DTexture9_UnlockRect(texture_2d[layer_num], 0); -} - -void osd_renderer_get_palette_buffer(UINT32 **buffer, int *width, int *pitch) -{ - D3DLOCKED_RECT locked_rect; - HRESULT hr; - - *buffer = NULL; - *pitch = 0; - *width = 0; - - hr = IDirect3DTexture9_LockRect(palette_2d, 0, &locked_rect, NULL, D3DLOCK_DISCARD); - if (!FAILED(hr)) - { - *buffer = (UINT32*)locked_rect.pBits; - *pitch = locked_rect.Pitch / 4; - *width = 256; - } -} - -void osd_renderer_free_palette_buffer(void) -{ - IDirect3DTexture9_UnlockRect(palette_2d, 0); -} - -void osd_renderer_get_priority_buffer(int layer_num, UINT8 **buffer, int *pitch) -{ - D3DLOCKED_RECT locked_rect; - HRESULT hr; - - *buffer = NULL; - *pitch = 0; - - hr = IDirect3DTexture9_LockRect(priority_2d[layer_num], 0, &locked_rect, NULL, D3DLOCK_DISCARD); - if (!FAILED(hr)) - { - *buffer = (UINT8*)locked_rect.pBits; - *pitch = locked_rect.Pitch; - } -} - -void osd_renderer_free_priority_buffer(int layer_num) -{ - IDirect3DTexture9_UnlockRect(priority_2d[layer_num], 0); -} - -void osd_renderer_draw_text(int x, int y, const char* string, DWORD color, BOOL shadow) -{ - RECT rect = { x, y, 496-1, 384-1 }; - RECT rect_s = { x+1, y+1, 496-1, 384-1 }; - if (shadow) - { - font->lpVtbl->DrawText(font, NULL, string, -1, &rect_s, 0, 0xFF000000); - } - - font->lpVtbl->DrawText(font, NULL, string, -1, &rect, 0, color); -} - - -/* - * void osd_renderer_set_coordinate_system(const MATRIX m); - * - * Applies the coordinate system matrix and makes adjustments so that the - * Model 3 coordinate system is properly handled. - * - * Parameters: - * m = Matrix. - */ - -void osd_renderer_set_coordinate_system( const MATRIX m ) -{ - memcpy( &view_matrix, m, sizeof(MATRIX) ); - view_matrix._22 = -view_matrix._22; -} - -static void cache_model(VERTEX *vb, VERTEX *vb_alpha, UINT32 *src, UINT32 address, int *verts, int *verts_alpha) -{ - float fixed_point_scale; - VERTEX vertex[4]; - VERTEX prev_vertex[4]; - int end, index, vb_index, vb_index_alpha; - int polygon_index = 0; - float uv_scale; - - int polygon = 0; - - int normal_num = 0; - - *verts = 0; - *verts_alpha = 0; - - //UINT32 *src = get_address( address ); - - if(src == NULL) - return FALSE; - - if(m3_config.step == 0x10) - fixed_point_scale = 32768.0f; - else - fixed_point_scale = 524288.0f; - - memset(prev_vertex, 0, sizeof(prev_vertex)); - end = 0; - index = 0; - vb_index = 0; - vb_index_alpha = 0; - - do { - UINT32 header[7]; - UINT32 entry[16]; - D3DCOLOR color, color2; - int transparency; - int i, num_vertices, num_old_vertices; - int v2, v; - int texture_format; - int texture_x, texture_y, tex_width, tex_height; - float nx, ny, nz; - float dot; - int polygon_id; - - for( i=0; i<7; i++) { - header[i] = src[index]; - index++; - } - - uv_scale = (header[1] & 0x40) ? 1.0f : 8.0f; - - polygon_id = (header[0] >> 10) & 0x3fff; - - if (polygon == 0 && (header[0] & 0xf) != 0) - return FALSE; - - if (header[6] == 0 /*|| header[0] & 0x300*/) - return FALSE; - - if ((header[0] & 0x300) == 0x300) - { - //printf("Polygon = %08X %08X %08X %08X %08X %08X %08X (address = %08X)\n", header[0], header[1], header[2], header[3], header[4], header[5], header[6], address); - } - - // Check if this is the last polygon - if(header[1] & 0x4) - end = 1; - - transparency = ((header[6] >> 18) & 0x1F) << 3; - if (header[6] & 0x800000) - { - color = 0xFF000000 | ((header[4] >> 8) & 0xffffff); - } - else - { - color = (transparency << 24) | ((header[4] >> 8) & 0xffffff); - } - - if (header[6] & 0x10000) - { - color2 = 0xffff0000 | (((header[6] >> 11) & 0x1F) << 3) << 16; - } - else - { - color2 = 0; - } - - if (header[6] & 0x4000000) - { - // texture enable - color2 |= 0x0000ff00; - } - - texture_x = 32 * (((header[4] & 0x1F) << 1) | ((header[5] >> 7) & 0x1)); - texture_y = 32 * ((header[5] & 0x1F) | ((header[4] >> 1) & 0x20)); - tex_width = 32 << ((header[3] >> 3) & 0x7); - tex_height = 32 << (header[3] & 0x7); - - texture_format = ((header[6] >> 7) & 0x7) ? 0 : 1; - if (texture_format == 0) texture_y += 2048; - - // Polygon normal - // Assuming 2.22 fixed-point. Is this correct ? - nx = (float)((int)header[1] >> 8) / 4194304.0f; - ny = (float)((int)header[2] >> 8) / 4194304.0f; - nz = (float)((int)header[3] >> 8) / 4194304.0f; - - // If bit 0x40 set this is a quad, otherwise a triangle - if(header[0] & 0x40) - num_vertices = 4; - else - num_vertices = 3; - - // How many vertices are reused - num_old_vertices = num_bits[header[0] & 0xF]; - - // Load reused vertices - v2 = 0; - for( v=0; v<4; v++) { - if( header[0] & (1 << v) ) { - memcpy( &vertex[v2], &prev_vertex[v], sizeof(VERTEX) ); - //vertex[v2].nx = prev_vertex[v].nx; - //vertex[v2].ny = prev_vertex[v].ny; - //vertex[v2].nz = prev_vertex[v].nz; - vertex[v2].color = color; - vertex[v2].color2 = color2; - vertex[v2].tx = texture_x; - vertex[v2].ty = texture_y; - vertex[v2].twidth = tex_width; - vertex[v2].theight = tex_height; - v2++; - } - } - - // Load vertex data - for( i=0; i<(num_vertices - num_old_vertices) * 4; i++) { - entry[i] = src[index]; - index++; - } - - // Load new vertices - for( v=0; v < (num_vertices - num_old_vertices); v++) { - int ix, iy, iz; - UINT16 tx,ty; - int xsize, ysize; - int v_index = v * 4; - - ix = entry[v_index]; - iy = entry[v_index + 1]; - iz = entry[v_index + 2]; - - if ((ix & 0xf0000000) == 0x70000000 || - (iy & 0xf0000000) == 0x70000000 || - (iz & 0xf0000000) == 0x70000000) - return FALSE; - - vertex[v2].x = (float)(ix) / fixed_point_scale; - vertex[v2].y = (float)(iy) / fixed_point_scale; - vertex[v2].z = (float)(iz) / fixed_point_scale; - - tx = (UINT16)((entry[v_index + 3] >> 16) & 0xFFFF); - ty = (UINT16)(entry[v_index + 3] & 0xFFFF); - xsize = 32 << ((header[3] >> 3) & 0x7); - ysize = 32 << (header[3] & 0x7); - - // Convert texture coordinates from 13.3 fixed-point to float - // Tex coords need to be divided by texture size because D3D wants them in - // range 0...1 - vertex[v2].u = ((float)(tx) / uv_scale) / (float)xsize; - vertex[v2].v = ((float)(ty) / uv_scale) / (float)ysize; - vertex[v2].nx = nx; - vertex[v2].ny = ny; - vertex[v2].nz = nz; - vertex[v2].color = color; - vertex[v2].color2 = color2; - vertex[v2].tx = texture_x; - vertex[v2].ty = texture_y; - vertex[v2].twidth = tex_width; - vertex[v2].theight = tex_height; - v2++; - - if (header[0] & 0x300) - { - //printf(" Vert: %08X %08X %08X %08X\n", entry[v_index+0], entry[v_index+1], entry[v_index+2], entry[v_index+3]); - } - } - - { - D3DXVECTOR3 e1, e2, cross, n; - e1.x = vertex[1].x - vertex[0].x; - e1.y = vertex[1].y - vertex[0].y; - e1.z = vertex[1].z - vertex[0].z; - e2.x = vertex[2].x - vertex[0].x; - e2.y = vertex[2].y - vertex[0].y; - e2.z = vertex[2].z - vertex[0].z; - - n.x = nx; - n.y = ny; - n.z = nz; - - D3DXVec3Cross(&cross, &e2, &e1); - D3DXVec3Normalize(&cross, &cross); - dot = D3DXVec3Dot(&n, &cross); - } - - // Transparent polygons - // Translucent texture (ARGB4444) - if((header[6] & 0x800000) == 0 || header[6] & 0x1 /* || header[6] & 0x80000000*/) - { - if (dot >= 0) - { - memcpy(&vb_alpha[vb_index_alpha+0], &vertex[0], sizeof(VERTEX)); - memcpy(&vb_alpha[vb_index_alpha+1], &vertex[1], sizeof(VERTEX)); - memcpy(&vb_alpha[vb_index_alpha+2], &vertex[2], sizeof(VERTEX)); - } - else - { - memcpy(&vb_alpha[vb_index_alpha+0], &vertex[2], sizeof(VERTEX)); - memcpy(&vb_alpha[vb_index_alpha+1], &vertex[1], sizeof(VERTEX)); - memcpy(&vb_alpha[vb_index_alpha+2], &vertex[0], sizeof(VERTEX)); - } - vb_index_alpha += 3; - - if (num_vertices > 3) - { - if (dot >= 0) - { - memcpy(&vb_alpha[vb_index_alpha+0], &vertex[0], sizeof(VERTEX)); - memcpy(&vb_alpha[vb_index_alpha+1], &vertex[2], sizeof(VERTEX)); - memcpy(&vb_alpha[vb_index_alpha+2], &vertex[3], sizeof(VERTEX)); - } - else - { - memcpy(&vb_alpha[vb_index_alpha+0], &vertex[3], sizeof(VERTEX)); - memcpy(&vb_alpha[vb_index_alpha+1], &vertex[2], sizeof(VERTEX)); - memcpy(&vb_alpha[vb_index_alpha+2], &vertex[0], sizeof(VERTEX)); - } - vb_index_alpha += 3; - } - } - else - { - if (dot >= 0) - { - memcpy(&vb[vb_index+0], &vertex[0], sizeof(VERTEX)); - memcpy(&vb[vb_index+1], &vertex[1], sizeof(VERTEX)); - memcpy(&vb[vb_index+2], &vertex[2], sizeof(VERTEX)); - } - else - { - memcpy(&vb[vb_index+0], &vertex[2], sizeof(VERTEX)); - memcpy(&vb[vb_index+1], &vertex[1], sizeof(VERTEX)); - memcpy(&vb[vb_index+2], &vertex[0], sizeof(VERTEX)); - } - vb_index += 3; - - if (num_vertices > 3) - { - if (dot >= 0) - { - memcpy(&vb[vb_index+0], &vertex[0], sizeof(VERTEX)); - memcpy(&vb[vb_index+1], &vertex[2], sizeof(VERTEX)); - memcpy(&vb[vb_index+2], &vertex[3], sizeof(VERTEX)); - } - else - { - memcpy(&vb[vb_index+0], &vertex[3], sizeof(VERTEX)); - memcpy(&vb[vb_index+1], &vertex[2], sizeof(VERTEX)); - memcpy(&vb[vb_index+2], &vertex[0], sizeof(VERTEX)); - } - vb_index += 3; - } - } - - - // Copy current vertex as previous vertex - memcpy(prev_vertex, vertex, sizeof(VERTEX) * 4); - - polygon++; - - } while(end == 0); - - *verts = vb_index / 3; - *verts_alpha = vb_index_alpha / 3; -} - -void osd_renderer_draw_model(UINT32 *mem, UINT32 address, int dynamic) -{ - int num_triangles, num_triangles_alpha; - VERTEX *vb, *vb_alpha; - D3DMATRIX world_view_proj, world_view; - D3DMATRIX matrix = d3d_matrix_stack_get_top(); - - // Make the world-view matrix - //D3DXMatrixMultiply(&world_view, &matrix, &view_matrix); - D3DXMatrixTranspose(&world_view, &matrix); - - // Make the world-view-projection matrix - D3DXMatrixMultiply(&world_view_proj, &matrix, &view_matrix); - D3DXMatrixMultiply(&world_view_proj, &world_view_proj, &projection_matrix); - D3DXMatrixTranspose(&world_view_proj, &world_view_proj); - - if (dynamic) - { - int vert_index = dynamic_mesh_cache_vertex_top; - int vert_index_alpha = dynamic_mesh_cache_vertex_top_alpha; - - IDirect3DVertexBuffer9_Lock(dynamic_vb, 0, 0, (void **)&vb, D3DLOCK_DISCARD); - IDirect3DVertexBuffer9_Lock(dynamic_vb_alpha, 0, 0, (void **)&vb_alpha, D3DLOCK_DISCARD); - cache_model(&vb[vert_index], &vb_alpha[vert_index_alpha], mem, address, &num_triangles, &num_triangles_alpha); - IDirect3DVertexBuffer9_Unlock(dynamic_vb_alpha); - IDirect3DVertexBuffer9_Unlock(dynamic_vb); - - dynamic_mesh_cache[dynamic_mesh_cache_top].vb_index = vert_index; - dynamic_mesh_cache[dynamic_mesh_cache_top].vb_index_alpha = vert_index_alpha; - dynamic_mesh_cache[dynamic_mesh_cache_top].num_vertices = num_triangles * 3; - dynamic_mesh_cache[dynamic_mesh_cache_top].num_vertices_alpha = num_triangles_alpha * 3; - - dynamic_mesh_cache_vertex_top += num_triangles * 3; - dynamic_mesh_cache_vertex_top_alpha += num_triangles_alpha * 3; - - memcpy(&dynamic_mesh_buffer[dynamic_mesh_buffer_top].normal, &world_view, sizeof(D3DMATRIX)); - memcpy(&dynamic_mesh_buffer[dynamic_mesh_buffer_top].transform, &world_view_proj, sizeof(D3DMATRIX)); - - dynamic_mesh_buffer[dynamic_mesh_buffer_top].lighting = current_light; - dynamic_mesh_buffer[dynamic_mesh_buffer_top].viewport = current_viewport; - - dynamic_mesh_buffer[dynamic_mesh_buffer_top].mesh_index = dynamic_mesh_cache_top; - - dynamic_mesh_cache_top++; - if (dynamic_mesh_cache_top >= NUM_DYNAMIC_MESHES) - { - printf("dynamic mesh cache overflow!\n"); - exit(1); - } - - dynamic_mesh_buffer_top++; - if (dynamic_mesh_buffer_top >= DYNAMIC_MESH_BUFFER_SIZE) - { - printf("dynamic mesh buffer overflow!\n"); - exit(1); - } - - if (dynamic_mesh_cache_vertex_top >= DYNAMIC_VB_SIZE) - { - printf("dynamic vertex cache overflow!\n"); - exit(1); - } - if (dynamic_mesh_cache_vertex_top_alpha >= DYNAMIC_VB_SIZE_ALPHA) - { - printf("dynamic vertex cache alpha overflow!\n"); - exit(1); - } - } - else - { - UINT16 mesh_index = static_mesh_index[address & 0xffffff]; - - if (mesh_index == 0xffff) - { - // cache new mesh - int vert_index = static_mesh_cache_vertex_top; - int vert_index_alpha = static_mesh_cache_vertex_top_alpha; - - int voffset = vert_index * sizeof(VERTEX); - int voffset_alpha = vert_index_alpha * sizeof(VERTEX); - int vsize = 10000 * sizeof(VERTEX); - - IDirect3DVertexBuffer9_Lock(static_vb, voffset, vsize, (void **)&vb, 0); - IDirect3DVertexBuffer9_Lock(static_vb_alpha, voffset_alpha, vsize, (void **)&vb_alpha, 0); - cache_model(&vb[0], &vb_alpha[0], mem, address, &num_triangles, &num_triangles_alpha); - IDirect3DVertexBuffer9_Unlock(static_vb_alpha); - IDirect3DVertexBuffer9_Unlock(static_vb); - - static_mesh_cache[static_mesh_cache_top].vb_index = vert_index; - static_mesh_cache[static_mesh_cache_top].vb_index_alpha = vert_index_alpha; - static_mesh_cache[static_mesh_cache_top].num_vertices = num_triangles * 3; - static_mesh_cache[static_mesh_cache_top].num_vertices_alpha = num_triangles_alpha * 3; - - static_mesh_cache_vertex_top += num_triangles * 3; - static_mesh_cache_vertex_top_alpha += num_triangles_alpha * 3; - - static_mesh_index[address & 0xffffff] = static_mesh_cache_top; - mesh_index = static_mesh_cache_top; - - static_mesh_cache_top++; - if (static_mesh_cache_top >= NUM_STATIC_MESHES) - { - printf("static mesh cache overflow!\n"); - exit(1); - } - - if (static_mesh_cache_vertex_top >= STATIC_VB_SIZE) - { - printf("static vertex cache overflow!\n"); - exit(1); - } - if (static_mesh_cache_vertex_top_alpha >= STATIC_VB_SIZE_ALPHA) - { - printf("static vertex cache alpha overflow!\n"); - exit(1); - } - } - - memcpy(&static_mesh_buffer[static_mesh_buffer_top].normal, &world_view, sizeof(D3DMATRIX)); - memcpy(&static_mesh_buffer[static_mesh_buffer_top].transform, &world_view_proj, sizeof(D3DMATRIX)); - - static_mesh_buffer[static_mesh_buffer_top].lighting = current_light; - static_mesh_buffer[static_mesh_buffer_top].viewport = current_viewport; - - static_mesh_buffer[static_mesh_buffer_top].mesh_index = mesh_index; - - static_mesh_buffer_top++; - if (static_mesh_buffer_top >= STATIC_MESH_BUFFER_SIZE) - { - printf("static mesh buffer overflow!\n"); - exit(1); - } - } -} - - -void osd_renderer_begin_3d_scene(void) -{ - dynamic_mesh_buffer_top = 0; - static_mesh_buffer_top = 0; - - dynamic_mesh_cache_top = 0; - dynamic_mesh_cache_vertex_top = 0; - dynamic_mesh_cache_vertex_top_alpha = 0; - - num_lights = 0; - num_viewports = 0; -} - -void osd_renderer_end_3d_scene(void) -{ - int i; - int end = 0; - int index = 0; - float mipmap_lod_bias; - - int selected_viewport = -1; - int selected_lighting = -1; - - IDirect3DDevice9_BeginScene(device); - - IDirect3DDevice9_SetVertexDeclaration(device, vertex_declaration); - IDirect3DDevice9_SetVertexShader(device, vshader); - IDirect3DDevice9_SetPixelShader(device, pshader); - IDirect3DDevice9_SetRenderState(device, D3DRS_CULLMODE, D3DCULL_NONE); - IDirect3DDevice9_SetRenderState(device, D3DRS_FILLMODE, D3DFILL_SOLID); - IDirect3DDevice9_SetRenderState(device, D3DRS_ZENABLE, D3DZB_USEW); - - IDirect3DDevice9_SetRenderState(device, D3DRS_ALPHATESTENABLE, FALSE); - - IDirect3DDevice9_SetRenderState(device, D3DRS_ALPHABLENDENABLE, FALSE); -// IDirect3DDevice9_SetRenderState(device, D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); -// IDirect3DDevice9_SetRenderState(device, D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); - IDirect3DDevice9_SetRenderState(device, D3DRS_ZWRITEENABLE, TRUE); - - IDirect3DDevice9_SetSamplerState(device, 0, D3DSAMP_MINFILTER, D3DTEXF_POINT); - IDirect3DDevice9_SetSamplerState(device, 0, D3DSAMP_MAGFILTER, D3DTEXF_POINT); - IDirect3DDevice9_SetSamplerState(device, 0, D3DSAMP_MAXMIPLEVEL, 0); - //IDirect3DDevice9_SetSamplerState(device, 0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR); - - //mipmap_lod_bias = -4.0f; - //IDirect3DDevice9_SetSamplerState( device, 0, D3DSAMP_MIPMAPLODBIAS, *(DWORD*)(&mipmap_lod_bias)); - - IDirect3DDevice9_SetTexture(device, 0, texture); - - IDirect3DDevice9_SetStreamSourceFreq(device, 0, 1); - IDirect3DDevice9_SetStreamSource(device, 0, static_vb, 0, sizeof(VERTEX)); - - set_viewport(0, 0, 496, 384); - - // render static meshes - for (i=0; i < static_mesh_buffer_top; i++) - { - D3DMATRIX *transform, *normal; - int num_triangles, vb_index; - - int mesh_index = static_mesh_buffer[i].mesh_index; - vb_index = static_mesh_cache[mesh_index].vb_index; - num_triangles = static_mesh_cache[mesh_index].num_vertices / 3; - - if (num_triangles > 0) - { - if (selected_viewport != static_mesh_buffer[i].viewport) - { - D3DVIEWPORT9 vp; - selected_viewport = static_mesh_buffer[i].viewport; - vp.X = viewport_params[selected_viewport].x; - vp.Y = viewport_params[selected_viewport].y; - vp.Width = viewport_params[selected_viewport].width; - vp.Height = viewport_params[selected_viewport].height; - vp.MinZ = min_z; - vp.MaxZ = max_z; - IDirect3DDevice9_SetViewport(device, &vp); - } - - transform = &static_mesh_buffer[i].transform; - normal = &static_mesh_buffer[i].normal; - - IDirect3DDevice9_SetVertexShaderConstantF(device, 0, (float*)transform, 4); - IDirect3DDevice9_SetVertexShaderConstantF(device, 4, (float*)normal, 4); - - if (selected_lighting != static_mesh_buffer[i].lighting) - { - selected_lighting = static_mesh_buffer[i].lighting; - IDirect3DDevice9_SetVertexShaderConstantF(device, 16, (float*)&lighting_params[selected_lighting].sun_vector, 1); - IDirect3DDevice9_SetVertexShaderConstantF(device, 17, (float*)&lighting_params[selected_lighting].sun_params, 1); - } - - IDirect3DDevice9_DrawPrimitive(device, D3DPT_TRIANGLELIST, vb_index, num_triangles); - } - } - - IDirect3DDevice9_SetStreamSourceFreq(device, 0, 1); - IDirect3DDevice9_SetStreamSource(device, 0, dynamic_vb, 0, sizeof(VERTEX)); - - // render dynamic meshes - for (i=0; i < dynamic_mesh_buffer_top; i++) - { - D3DMATRIX *transform, *normal; - int num_triangles, vb_index; - - int mesh_index = dynamic_mesh_buffer[i].mesh_index; - vb_index = dynamic_mesh_cache[mesh_index].vb_index; - num_triangles = dynamic_mesh_cache[mesh_index].num_vertices / 3; - - if (num_triangles > 0) - { - - if (selected_viewport != dynamic_mesh_buffer[i].viewport) - { - D3DVIEWPORT9 vp; - selected_viewport = dynamic_mesh_buffer[i].viewport; - vp.X = viewport_params[selected_viewport].x; - vp.Y = viewport_params[selected_viewport].y; - vp.Width = viewport_params[selected_viewport].width; - vp.Height = viewport_params[selected_viewport].height; - vp.MinZ = min_z; - vp.MaxZ = max_z; - IDirect3DDevice9_SetViewport(device, &vp); - } - - transform = &dynamic_mesh_buffer[i].transform; - normal = &dynamic_mesh_buffer[i].normal; - - IDirect3DDevice9_SetVertexShaderConstantF(device, 0, (float*)transform, 4); - IDirect3DDevice9_SetVertexShaderConstantF(device, 4, (float*)normal, 4); - - if (selected_lighting != dynamic_mesh_buffer[i].lighting) - { - selected_lighting = dynamic_mesh_buffer[i].lighting; - IDirect3DDevice9_SetVertexShaderConstantF(device, 16, (float*)&lighting_params[selected_lighting].sun_vector, 1); - IDirect3DDevice9_SetVertexShaderConstantF(device, 17, (float*)&lighting_params[selected_lighting].sun_params, 1); - } - - IDirect3DDevice9_DrawPrimitive(device, D3DPT_TRIANGLELIST, vb_index, num_triangles); - } - } - - // render alpha polys - IDirect3DDevice9_SetRenderState(device, D3DRS_ALPHABLENDENABLE, TRUE); - IDirect3DDevice9_SetRenderState(device, D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); - IDirect3DDevice9_SetRenderState(device, D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); - IDirect3DDevice9_SetRenderState(device, D3DRS_ZWRITEENABLE, FALSE); - - IDirect3DDevice9_SetStreamSourceFreq(device, 0, 1); - IDirect3DDevice9_SetStreamSource(device, 0, dynamic_vb_alpha, 0, sizeof(VERTEX)); - - // render dynamic meshes - //for (i=dynamic_mesh_buffer_top-1; i >= 0; i--) - for (i=0; i < dynamic_mesh_buffer_top; i++) - { - D3DMATRIX *transform, *normal; - int num_triangles, vb_index; - - int mesh_index = dynamic_mesh_buffer[i].mesh_index; - vb_index = dynamic_mesh_cache[mesh_index].vb_index_alpha; - num_triangles = dynamic_mesh_cache[mesh_index].num_vertices_alpha / 3; - - if (num_triangles > 0) - { - if (selected_viewport != dynamic_mesh_buffer[i].viewport) - { - D3DVIEWPORT9 vp; - selected_viewport = dynamic_mesh_buffer[i].viewport; - vp.X = viewport_params[selected_viewport].x; - vp.Y = viewport_params[selected_viewport].y; - vp.Width = viewport_params[selected_viewport].width; - vp.Height = viewport_params[selected_viewport].height; - vp.MinZ = min_z; - vp.MaxZ = max_z; - IDirect3DDevice9_SetViewport(device, &vp); - } - - transform = &dynamic_mesh_buffer[i].transform; - normal = &dynamic_mesh_buffer[i].normal; - - IDirect3DDevice9_SetVertexShaderConstantF(device, 0, (float*)transform, 4); - IDirect3DDevice9_SetVertexShaderConstantF(device, 4, (float*)normal, 4); - - if (selected_lighting != dynamic_mesh_buffer[i].lighting) - { - selected_lighting = dynamic_mesh_buffer[i].lighting; - IDirect3DDevice9_SetVertexShaderConstantF(device, 16, (float*)&lighting_params[selected_lighting].sun_vector, 1); - IDirect3DDevice9_SetVertexShaderConstantF(device, 17, (float*)&lighting_params[selected_lighting].sun_params, 1); - } - - IDirect3DDevice9_DrawPrimitive(device, D3DPT_TRIANGLELIST, vb_index, num_triangles); - } - } - - IDirect3DDevice9_SetStreamSourceFreq(device, 0, 1); - IDirect3DDevice9_SetStreamSource(device, 0, static_vb_alpha, 0, sizeof(VERTEX)); - - // render static meshes - //for (i=static_mesh_buffer_top-1; i >= 0; i--) - for (i=0; i < static_mesh_buffer_top; i++) - { - D3DMATRIX *transform, *normal; - int num_triangles, vb_index; - - int mesh_index = static_mesh_buffer[i].mesh_index; - vb_index = static_mesh_cache[mesh_index].vb_index_alpha; - num_triangles = static_mesh_cache[mesh_index].num_vertices_alpha / 3; - - if (num_triangles > 0) - { - if (selected_viewport != static_mesh_buffer[i].viewport) - { - D3DVIEWPORT9 vp; - selected_viewport = static_mesh_buffer[i].viewport; - vp.X = viewport_params[selected_viewport].x; - vp.Y = viewport_params[selected_viewport].y; - vp.Width = viewport_params[selected_viewport].width; - vp.Height = viewport_params[selected_viewport].height; - vp.MinZ = min_z; - vp.MaxZ = max_z; - IDirect3DDevice9_SetViewport(device, &vp); - } - - transform = &static_mesh_buffer[i].transform; - normal = &static_mesh_buffer[i].normal; - - IDirect3DDevice9_SetVertexShaderConstantF(device, 0, (float*)transform, 4); - IDirect3DDevice9_SetVertexShaderConstantF(device, 4, (float*)normal, 4); - - if (selected_lighting != static_mesh_buffer[i].lighting) - { - selected_lighting = static_mesh_buffer[i].lighting; - IDirect3DDevice9_SetVertexShaderConstantF(device, 16, (float*)&lighting_params[selected_lighting].sun_vector, 1); - IDirect3DDevice9_SetVertexShaderConstantF(device, 17, (float*)&lighting_params[selected_lighting].sun_params, 1); - } - - IDirect3DDevice9_DrawPrimitive(device, D3DPT_TRIANGLELIST, vb_index, num_triangles); - } - } - - IDirect3DDevice9_EndScene(device); -} - -void osd_renderer_set_light( int light_num, LIGHT* param ) -{ - switch(param->type) - { - case LIGHT_PARALLEL: - lighting_params[num_lights].sun_vector.x = -param->u * view_matrix._11; - lighting_params[num_lights].sun_vector.y = param->v * view_matrix._22; - lighting_params[num_lights].sun_vector.z = -param->w * view_matrix._33; - lighting_params[num_lights].sun_vector.w = 1.0f; - - lighting_params[num_lights].sun_params.x = param->diffuse_intensity; - lighting_params[num_lights].sun_params.y = param->ambient_intensity; - break; - - default: - error("Direct3D error: Unsupported light type: %d",param->type); - } - - current_light = num_lights; - - num_lights++; - if (num_lights >= MAX_LIGHTS) - { - error("Too many lights!"); - } -} - -void osd_renderer_set_viewport(const VIEWPORT* vp) -{ - float fov = D3DXToRadian( (float)(vp->up + vp->down) ); - float aspect_ratio = (float)((float)vp->width / (float)vp->height); - - float x = vp->x; - float y = vp->y; - float w = vp->width; - float h = vp->height; - - viewport_params[num_viewports].x = x; - viewport_params[num_viewports].y = y; - viewport_params[num_viewports].width = w; - viewport_params[num_viewports].height = h; - - current_viewport = num_viewports; - - num_viewports++; - if (num_viewports >= MAX_VIEWPORTS) - { - error("Too many viewports!"); - } - - D3DXMatrixPerspectiveFovLH(&projection_matrix, fov, aspect_ratio, min_z, max_z); -} - -//////////////////////// -// Matrix Stack // -//////////////////////// - -static D3DMATRIX matrix_to_d3d( const MATRIX m ) -{ - D3DMATRIX c; - memcpy( &c, m, sizeof(MATRIX) ); - - return c; -} - -static int stack_ptr = 0; - -static void d3d_matrix_stack_init(void) -{ - matrix_stack->lpVtbl->LoadIdentity(matrix_stack); -} - -static D3DXMATRIX d3d_matrix_stack_get_top(void) -{ - D3DXMATRIX *m = matrix_stack->lpVtbl->GetTop(matrix_stack); - return *m; -} - -/* - * void osd_renderer_push_matrix(void); - * - * Pushes a matrix on to the stack. The matrix pushed is the former top of the - * stack. - */ - -void osd_renderer_push_matrix(void) -{ - stack_ptr++; - matrix_stack->lpVtbl->Push(matrix_stack); - - if (stack_ptr >= 256) - { - error("Matrix stack overflow\n"); - } -} - -/* - * void osd_renderer_pop_matrix(void); - * - * Pops a matrix off the top of the stack. - */ - -void osd_renderer_pop_matrix(void) -{ - stack_ptr--; - if( stack_ptr >= 0) - matrix_stack->lpVtbl->Pop(matrix_stack); -} - -/* - * void osd_renderer_multiply_matrix(MATRIX m); - * - * Multiplies the top of the matrix stack by the specified matrix - * - * Parameters: - * m = Matrix to multiply. - */ - -void osd_renderer_multiply_matrix( MATRIX m ) -{ - D3DMATRIX x = matrix_to_d3d( m ); - matrix_stack->lpVtbl->MultMatrixLocal(matrix_stack, &x ); -} - -/* - * void osd_renderer_translate_matrix(float x, float y, float z); - * - * Translates the top of the matrix stack. - * - * Parameters: - * x = Translation along X axis. - * y = Y axis. - * z = Z axis. - */ - -void osd_renderer_translate_matrix( float x, float y, float z ) -{ - //stack->lpVtbl->TranslateLocal(stack, x, y, z ); - D3DMATRIX t; - t._11 = 1.0f; t._12 = 0.0f; t._13 = 0.0f; t._14 = 0.0f; - t._21 = 0.0f; t._22 = 1.0f; t._23 = 0.0f; t._24 = 0.0f; - t._31 = 0.0f; t._32 = 0.0f; t._33 = 1.0f; t._34 = 0.0f; - t._41 = x; t._42 = y; t._43 = z; t._44 = 1.0f; - matrix_stack->lpVtbl->MultMatrixLocal(matrix_stack,&t); -} - -// Mipmap starting positions for each level -static const int mipmap_xpos[] = -{ 0, 1024, 1536, 1792, 1920, 1984, 2016, 2032, 2040, 2044, 2046, 2047 }; -static const int mipmap_ypos[] = -{ 0, 512, 768, 896, 960, 992, 1008, 1016, 1020, 1022, 1023, 0 }; - -void renderer_upload_texture(int x, int y, int u, int v, int width, int height, void *data, int mip_level) -{ - HRESULT hr; - int i, j; - D3DLOCKED_RECT rect; - UINT16 *s = (UINT16*)data; - UINT32 *d; - int pitch; - RECT texture_rect; - - if (mip_level > 3) - return; - - /*hr = IDirect3DTexture9_LockRect(texture, 0, &rect, NULL, 0); - d = (UINT32*)rect.pBits; - pitch = rect.Pitch / 4; - - for (j=y; j < y+height; j++) - { - int index = j * pitch; - int u = x; - for (i=x; i < x+width; i++) - { - UINT16 c = s[(j * 2048) + u]; - int r = ((c >> 10) & 0x1f) << 3; - int g = ((c >> 5) & 0x1f) << 3; - int b = ((c >> 0) & 0x1f) << 3; - d[index+i] = 0xff000000 | (r << 16) | (g << 8) | (b); - u++; - } - } - - for (j=y; j < y+height; j++) - { - int index = (j+2048) * pitch; - int u = x; - for (i=x; i < x+width; i++) - { - UINT16 c = s[(j * 2048) + u]; - int r = ((c >> 12) & 0xf) << 4; - int g = ((c >> 8) & 0xf) << 4; - int b = ((c >> 4) & 0xf) << 4; - int a = ((c >> 0) & 0xf) << 4; - d[index+i] = (a << 24) | (r << 16) | (g << 8) | (b); - u++; - } - } - - IDirect3DTexture9_UnlockRect(texture, 0);*/ - - u >>= mip_level; - v >>= mip_level; - width >>= mip_level; - height >>= mip_level; - - texture_rect.left = u; - texture_rect.top = v; - texture_rect.right = u + width; - texture_rect.bottom = v + height; - - hr = IDirect3DTexture9_LockRect(texture, mip_level, &rect, &texture_rect, 0); - if (!FAILED(hr)) - { - d = (UINT32*)rect.pBits; - pitch = rect.Pitch / 4; - - for (j=0; j < height; j++) - { - int index = j * pitch; - for (i=0; i < width; i++) - { - UINT16 c = s[((j+y) * 2048) + x + i]; - int r = ((c >> 10) & 0x1f) << 3; - int g = ((c >> 5) & 0x1f) << 3; - int b = ((c >> 0) & 0x1f) << 3; - int a = (c & 0x8000) ? 0 : 0xff; - d[index+i] = (a << 24) | (r << 16) | (g << 8) | (b); - } - } - } - IDirect3DTexture9_UnlockRect(texture, mip_level); - - texture_rect.left = u; - texture_rect.top = v + (2048 >> mip_level); - texture_rect.right = u + width; - texture_rect.bottom = v + (2048 >> mip_level) + height; - - hr = IDirect3DTexture9_LockRect(texture, mip_level, &rect, &texture_rect, 0); - if (!FAILED(hr)) - { - d = (UINT32*)rect.pBits; - pitch = rect.Pitch / 4; - for (j=0; j < height; j++) - { - int index = j * pitch; - for (i=0; i < width; i++) - { - UINT16 c = s[((j+y) * 2048) + x + i]; - int r = ((c >> 12) & 0xf) << 4; - int g = ((c >> 8) & 0xf) << 4; - int b = ((c >> 4) & 0xf) << 4; - int a = ((c >> 0) & 0xf) << 4; - d[index+i] = (a << 24) | (r << 16) | (g << 8) | (b); - } - } - } - IDirect3DTexture9_UnlockRect(texture, mip_level); -} - -void osd_renderer_invalidate_textures(UINT x, UINT y, int u, int v, UINT w, UINT h, UINT8 *texture_sheet, int miplevel) -{ - renderer_upload_texture(x, y, u, v, w, h, texture_sheet, miplevel); -} - -UINT32 osd_renderer_get_features(void) -{ - return RENDERER_FEATURE_PALETTE | RENDERER_FEATURE_PRIORITY; -} \ No newline at end of file diff --git a/win32/dx_render.h b/win32/dx_render.h deleted file mode 100644 index 4352af2..0000000 --- a/win32/dx_render.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Sega Model 3 Emulator - * Copyright (C) 2003 Bart Trzynadlowski, Ville Linde, Stefano Teso - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License Version 2 as published - * by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program (license.txt); if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/* - * win32/dx_render.h - * - * DirectX9 Renderer interface - */ - -#include "d3d9.h" -#include "d3dx9.h" - -#define RGB_REG 0xFFFF0000 -#define RGB_GREEN 0xFF00FF00 -#define RGB_BLUE 0xFF0000FF -#define RGB_YELLOW 0xFFFFFF00 -#define RGB_CYAN 0xFF00FFFF - -BOOL d3d_init(HWND); -BOOL d3d_pre_init(void); -void d3d_shutdown(void); -D3DXMATRIX d3d_matrix_stack_get_top(void); -void d3d_matrix_stack_init(void); \ No newline at end of file diff --git a/win32/osd.h b/win32/osd.h deleted file mode 100644 index c068194..0000000 --- a/win32/osd.h +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Sega Model 3 Emulator - * Copyright (C) 2003 Bart Trzynadlowski, Ville Linde, Stefano Teso - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License Version 2 as published - * by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program (license.txt); if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/* - * win32/osd.h - * - * Win32 OSD header: Defines everything an OSD header needs to provide and - * includes some Win32-specific files. - */ - -#ifndef INCLUDED_WIN32_OSD_H -#define INCLUDED_WIN32_OSD_H - -/******************************************************************/ -/* Win32-Specific Includes */ -/******************************************************************/ - -#define WIN32_LEAN_AND_MEAN -#include - -/******************************************************************/ -/* OSD Definitions */ -/******************************************************************/ - -#ifdef _MSC_VER -#define INLINE static _inline -#endif - -#ifdef __GCC__ -#define INLINE static __inline__ -#endif - -/******************************************************************/ -/* OSD Fundamental Data Types */ -/******************************************************************/ - -typedef unsigned int FLAGS; // for storage of bit flags - -#ifdef __GCC__ - -typedef int BOOL; // 0 (false) or non-zero (true) - -typedef signed int INT; // optimal signed integer -typedef unsigned int UINT; // optimal unsigned integer -typedef signed char CHAR; // signed character (for text) -typedef unsigned char UCHAR; // unsigned character - -typedef signed char INT8; -typedef unsigned char UINT8; -typedef signed short INT16; -typedef unsigned short UINT16; -typedef signed int INT32; -typedef unsigned int UINT32; - -#ifdef _MSC_VER // Microsoft VisualC++ (for versions 6.0 or older, non-C99) -typedef signed __int64 INT64; -typedef unsigned __int64 UINT64; -#else // assume a C99-compliant compiler -typedef signed long long INT64; -typedef unsigned long long UINT64; -#endif - -#endif - -typedef float FLOAT32; // single precision (32-bit) -typedef double FLOAT64; // double precision (64-bit) - -#endif // INCLUDED_WIN32_OSD_H - diff --git a/win32/pixel_shader.ps b/win32/pixel_shader.ps deleted file mode 100644 index c536d72..0000000 --- a/win32/pixel_shader.ps +++ /dev/null @@ -1,57 +0,0 @@ -ps_3_0 - -; input declarations -dcl_color0 v0.xyzw -dcl_texcoord0 v1.xyzw -dcl_texcoord1 v2.xyzw -dcl_texcoord2 v3.xyzw -dcl_color1 v4.xyzw - -dcl_2d s0 - -; constants -def c0, 1.0f, 0.0f, 0.0f, 0.5f -def c1, 0.00048828125f, 0.000244140625f, 0.0f, 0.0f - - - -rcp r30, v2.z ; calculate 1/width -rcp r31, v2.w ; calculate 1/height - -mov r31.xy, c0.yy ; r31.xy <- 0,0 \ hopefully co-issue... -mov r31.z, r30.z ; / - -add r1.xyzw, v1.xyxy, r31.yyzy ; texel[0][0], texel[1][0] -add r2.xyzw, v1.xyxy, r31.ywzw ; texel[0][1], texel[1][1] -frc r1.xyzw, r1.xyzw -frc r2.xyzw, r2.xyzw -mul r3, r1, v2.zwzw ; mul by width, height -mul r4, r2, v2.zwzw ; mul by width, height -add r10, r3, v2.xyxy ; add tex base -add r11, r4, v2.xyxy ; add tex base -mul r10, r10, c1.xyxy ; div by 2048 -mul r11, r11, c1.xyxy ; div by 2048 - -frc r18, r3 - -texld r5, r10.xy, s0 -texld r6, r10.zw, s0 -texld r7, r11.xy, s0 -texld r8, r11.zw, s0 - -lrp r12, r18.xxxx, r6, r5 -lrp r13, r18.xxxx, r8, r7 -lrp r15, r18.yyyy, r13, r12 - -;if_lt v0.a, c0.x ; if polygon alpha is enabled use that instead of texture alpha -; mov r15.a, v0.a -;endif - -if_ne v4.x, c0.y ; if v4.x == 0 -> color, if v4.x != -> texture - mul_sat r15, v0, r15 ; modulate with color -else - mov r15, v0 -endif -;mov r15, v0 - -mov oC0, r15 \ No newline at end of file diff --git a/win32/pixel_shader_2d.ps b/win32/pixel_shader_2d.ps deleted file mode 100644 index bcfd213..0000000 --- a/win32/pixel_shader_2d.ps +++ /dev/null @@ -1,38 +0,0 @@ -ps_3_0 - -; parameters -; c8 = color offset -; c9 = (X,Y) scroll position, (Z) 0 = rendering background, 1 = rendering foreground - -; input declarations -dcl_color v0.xyzw -dcl_texcoord0 v1.xy - -dcl_2d s0 -dcl_2d s1 -dcl_2d s2 - -def c0, 1.0, 0.0, 1.0f, 1.0f -def c1, 0.5, 0.0, 0.0, 0.0 - -add r5, v1.xy, c9.xy ; add scroll position -frc r5, r5 ; wrap - -texld r0, r5, s0 - -texld r10, v1, s2 ; load priority value -mov r11.r, c9.z - -mov r1.xy, r0.ga -texld r2, r1, s1 - -add r2.rgb, r2.rgb, c8.yzw -min r2, r2, c0.xxxx ; clamp to 1.0 -max r2, r2, c0.yyyy ; clamp to 0.0 - -if_eq r11.r, c0.y ; invert priority if rendering foreground - sub r10.r, c0.x, r10.r -endif -mul r2.a, r2.a, r10.r ; multiply alpha with priority - -mov oC0, r2 \ No newline at end of file diff --git a/win32/shaders.h b/win32/shaders.h deleted file mode 100644 index dcdffe7..0000000 --- a/win32/shaders.h +++ /dev/null @@ -1,106 +0,0 @@ -unsigned char pixel_shader_source[524] = -{ - 0x00, 0x03, 0xFF, 0xFF, 0x1F, 0x00, 0x00, 0x02, 0x0A, 0x00, 0x00, 0x80, 0x00, 0x00, 0x0F, 0x90, - 0x1F, 0x00, 0x00, 0x02, 0x05, 0x00, 0x00, 0x80, 0x01, 0x00, 0x0F, 0x90, 0x1F, 0x00, 0x00, 0x02, - 0x05, 0x00, 0x01, 0x80, 0x02, 0x00, 0x0F, 0x90, 0x1F, 0x00, 0x00, 0x02, 0x05, 0x00, 0x02, 0x80, - 0x03, 0x00, 0x0F, 0x90, 0x1F, 0x00, 0x00, 0x02, 0x0A, 0x00, 0x01, 0x80, 0x04, 0x00, 0x0F, 0x90, - 0x1F, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x90, 0x00, 0x08, 0x0F, 0xA0, 0x51, 0x00, 0x00, 0x05, - 0x00, 0x00, 0x0F, 0xA0, 0x00, 0x00, 0x80, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x3F, 0x51, 0x00, 0x00, 0x05, 0x01, 0x00, 0x0F, 0xA0, 0x00, 0x00, 0x00, 0x3A, - 0x00, 0x00, 0x80, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x02, - 0x1E, 0x00, 0x0F, 0x80, 0x02, 0x00, 0xAA, 0x90, 0x06, 0x00, 0x00, 0x02, 0x1F, 0x00, 0x0F, 0x80, - 0x02, 0x00, 0xFF, 0x90, 0x01, 0x00, 0x00, 0x02, 0x1F, 0x00, 0x03, 0x80, 0x00, 0x00, 0x55, 0xA0, - 0x01, 0x00, 0x00, 0x02, 0x1F, 0x00, 0x04, 0x80, 0x1E, 0x00, 0xAA, 0x80, 0x02, 0x00, 0x00, 0x03, - 0x01, 0x00, 0x0F, 0x80, 0x01, 0x00, 0x44, 0x90, 0x1F, 0x00, 0x65, 0x80, 0x02, 0x00, 0x00, 0x03, - 0x02, 0x00, 0x0F, 0x80, 0x01, 0x00, 0x44, 0x90, 0x1F, 0x00, 0xED, 0x80, 0x13, 0x00, 0x00, 0x02, - 0x01, 0x00, 0x0F, 0x80, 0x01, 0x00, 0xE4, 0x80, 0x13, 0x00, 0x00, 0x02, 0x02, 0x00, 0x0F, 0x80, - 0x02, 0x00, 0xE4, 0x80, 0x05, 0x00, 0x00, 0x03, 0x03, 0x00, 0x0F, 0x80, 0x01, 0x00, 0xE4, 0x80, - 0x02, 0x00, 0xEE, 0x90, 0x05, 0x00, 0x00, 0x03, 0x04, 0x00, 0x0F, 0x80, 0x02, 0x00, 0xE4, 0x80, - 0x02, 0x00, 0xEE, 0x90, 0x02, 0x00, 0x00, 0x03, 0x0A, 0x00, 0x0F, 0x80, 0x03, 0x00, 0xE4, 0x80, - 0x02, 0x00, 0x44, 0x90, 0x02, 0x00, 0x00, 0x03, 0x0B, 0x00, 0x0F, 0x80, 0x04, 0x00, 0xE4, 0x80, - 0x02, 0x00, 0x44, 0x90, 0x05, 0x00, 0x00, 0x03, 0x0A, 0x00, 0x0F, 0x80, 0x0A, 0x00, 0xE4, 0x80, - 0x01, 0x00, 0x44, 0xA0, 0x05, 0x00, 0x00, 0x03, 0x0B, 0x00, 0x0F, 0x80, 0x0B, 0x00, 0xE4, 0x80, - 0x01, 0x00, 0x44, 0xA0, 0x13, 0x00, 0x00, 0x02, 0x12, 0x00, 0x0F, 0x80, 0x03, 0x00, 0xE4, 0x80, - 0x42, 0x00, 0x00, 0x03, 0x05, 0x00, 0x0F, 0x80, 0x0A, 0x00, 0x54, 0x80, 0x00, 0x08, 0xE4, 0xA0, - 0x42, 0x00, 0x00, 0x03, 0x06, 0x00, 0x0F, 0x80, 0x0A, 0x00, 0xFE, 0x80, 0x00, 0x08, 0xE4, 0xA0, - 0x42, 0x00, 0x00, 0x03, 0x07, 0x00, 0x0F, 0x80, 0x0B, 0x00, 0x54, 0x80, 0x00, 0x08, 0xE4, 0xA0, - 0x42, 0x00, 0x00, 0x03, 0x08, 0x00, 0x0F, 0x80, 0x0B, 0x00, 0xFE, 0x80, 0x00, 0x08, 0xE4, 0xA0, - 0x12, 0x00, 0x00, 0x04, 0x0C, 0x00, 0x0F, 0x80, 0x12, 0x00, 0x00, 0x80, 0x06, 0x00, 0xE4, 0x80, - 0x05, 0x00, 0xE4, 0x80, 0x12, 0x00, 0x00, 0x04, 0x0D, 0x00, 0x0F, 0x80, 0x12, 0x00, 0x00, 0x80, - 0x08, 0x00, 0xE4, 0x80, 0x07, 0x00, 0xE4, 0x80, 0x12, 0x00, 0x00, 0x04, 0x0F, 0x00, 0x0F, 0x80, - 0x12, 0x00, 0x55, 0x80, 0x0D, 0x00, 0xE4, 0x80, 0x0C, 0x00, 0xE4, 0x80, 0x29, 0x00, 0x05, 0x02, - 0x04, 0x00, 0x00, 0x90, 0x00, 0x00, 0x55, 0xA0, 0x05, 0x00, 0x00, 0x03, 0x0F, 0x00, 0x1F, 0x80, - 0x00, 0x00, 0xE4, 0x90, 0x0F, 0x00, 0xE4, 0x80, 0x2A, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x02, - 0x0F, 0x00, 0x0F, 0x80, 0x00, 0x00, 0xE4, 0x90, 0x2B, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x02, - 0x00, 0x08, 0x0F, 0x80, 0x0F, 0x00, 0xE4, 0x80, 0xFF, 0xFF, 0x00, 0x00, -}; - -unsigned char pixel_shader_2d_source[324] = -{ - 0x00, 0x03, 0xFF, 0xFF, 0x1F, 0x00, 0x00, 0x02, 0x0A, 0x00, 0x00, 0x80, 0x00, 0x00, 0x0F, 0x90, - 0x1F, 0x00, 0x00, 0x02, 0x05, 0x00, 0x00, 0x80, 0x01, 0x00, 0x03, 0x90, 0x1F, 0x00, 0x00, 0x02, - 0x00, 0x00, 0x00, 0x90, 0x00, 0x08, 0x0F, 0xA0, 0x1F, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x90, - 0x01, 0x08, 0x0F, 0xA0, 0x1F, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x90, 0x02, 0x08, 0x0F, 0xA0, - 0x51, 0x00, 0x00, 0x05, 0x00, 0x00, 0x0F, 0xA0, 0x00, 0x00, 0x80, 0x3F, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x80, 0x3F, 0x00, 0x00, 0x80, 0x3F, 0x51, 0x00, 0x00, 0x05, 0x01, 0x00, 0x0F, 0xA0, - 0x00, 0x00, 0x00, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x02, 0x00, 0x00, 0x03, 0x05, 0x00, 0x0F, 0x80, 0x01, 0x00, 0x54, 0x90, 0x09, 0x00, 0x54, 0xA0, - 0x13, 0x00, 0x00, 0x02, 0x05, 0x00, 0x0F, 0x80, 0x05, 0x00, 0xE4, 0x80, 0x42, 0x00, 0x00, 0x03, - 0x00, 0x00, 0x0F, 0x80, 0x05, 0x00, 0xE4, 0x80, 0x00, 0x08, 0xE4, 0xA0, 0x42, 0x00, 0x00, 0x03, - 0x0A, 0x00, 0x0F, 0x80, 0x01, 0x00, 0xE4, 0x90, 0x02, 0x08, 0xE4, 0xA0, 0x01, 0x00, 0x00, 0x02, - 0x0B, 0x00, 0x01, 0x80, 0x09, 0x00, 0xAA, 0xA0, 0x01, 0x00, 0x00, 0x02, 0x01, 0x00, 0x03, 0x80, - 0x00, 0x00, 0xFD, 0x80, 0x42, 0x00, 0x00, 0x03, 0x02, 0x00, 0x0F, 0x80, 0x01, 0x00, 0xE4, 0x80, - 0x01, 0x08, 0xE4, 0xA0, 0x02, 0x00, 0x00, 0x03, 0x02, 0x00, 0x07, 0x80, 0x02, 0x00, 0xA4, 0x80, - 0x08, 0x00, 0xF9, 0xA0, 0x0A, 0x00, 0x00, 0x03, 0x02, 0x00, 0x0F, 0x80, 0x02, 0x00, 0xE4, 0x80, - 0x00, 0x00, 0x00, 0xA0, 0x0B, 0x00, 0x00, 0x03, 0x02, 0x00, 0x0F, 0x80, 0x02, 0x00, 0xE4, 0x80, - 0x00, 0x00, 0x55, 0xA0, 0x29, 0x00, 0x02, 0x02, 0x0B, 0x00, 0x00, 0x80, 0x00, 0x00, 0x55, 0xA0, - 0x02, 0x00, 0x00, 0x03, 0x0A, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0xA0, 0x0A, 0x00, 0x00, 0x81, - 0x2B, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x03, 0x02, 0x00, 0x08, 0x80, 0x02, 0x00, 0xFF, 0x80, - 0x0A, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x02, 0x00, 0x08, 0x0F, 0x80, 0x02, 0x00, 0xE4, 0x80, - 0xFF, 0xFF, 0x00, 0x00, -}; - -unsigned char vertex_shader_source[500] = -{ - 0x00, 0x03, 0xFE, 0xFF, 0x1F, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x0F, 0x90, - 0x1F, 0x00, 0x00, 0x02, 0x0A, 0x00, 0x00, 0x80, 0x01, 0x00, 0x0F, 0x90, 0x1F, 0x00, 0x00, 0x02, - 0x05, 0x00, 0x00, 0x80, 0x02, 0x00, 0x0F, 0x90, 0x1F, 0x00, 0x00, 0x02, 0x05, 0x00, 0x01, 0x80, - 0x03, 0x00, 0x0F, 0x90, 0x1F, 0x00, 0x00, 0x02, 0x05, 0x00, 0x02, 0x80, 0x04, 0x00, 0x0F, 0x90, - 0x1F, 0x00, 0x00, 0x02, 0x03, 0x00, 0x00, 0x80, 0x05, 0x00, 0x0F, 0x90, 0x1F, 0x00, 0x00, 0x02, - 0x0A, 0x00, 0x01, 0x80, 0x06, 0x00, 0x0F, 0x90, 0x1F, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x80, - 0x00, 0x00, 0x0F, 0xE0, 0x1F, 0x00, 0x00, 0x02, 0x0A, 0x00, 0x00, 0x80, 0x01, 0x00, 0x0F, 0xE0, - 0x1F, 0x00, 0x00, 0x02, 0x05, 0x00, 0x00, 0x80, 0x02, 0x00, 0x0F, 0xE0, 0x1F, 0x00, 0x00, 0x02, - 0x05, 0x00, 0x01, 0x80, 0x03, 0x00, 0x0F, 0xE0, 0x1F, 0x00, 0x00, 0x02, 0x05, 0x00, 0x02, 0x80, - 0x04, 0x00, 0x0F, 0xE0, 0x1F, 0x00, 0x00, 0x02, 0x0A, 0x00, 0x01, 0x80, 0x05, 0x00, 0x0F, 0xE0, - 0x51, 0x00, 0x00, 0x05, 0x80, 0x00, 0x0F, 0xA0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3F, - 0x00, 0x00, 0x00, 0x3F, 0x00, 0x00, 0x80, 0x3F, 0x09, 0x00, 0x00, 0x03, 0x14, 0x00, 0x01, 0x80, - 0x00, 0x00, 0xE4, 0x90, 0x00, 0x00, 0xE4, 0xA0, 0x09, 0x00, 0x00, 0x03, 0x14, 0x00, 0x02, 0x80, - 0x00, 0x00, 0xE4, 0x90, 0x01, 0x00, 0xE4, 0xA0, 0x09, 0x00, 0x00, 0x03, 0x14, 0x00, 0x04, 0x80, - 0x00, 0x00, 0xE4, 0x90, 0x02, 0x00, 0xE4, 0xA0, 0x09, 0x00, 0x00, 0x03, 0x14, 0x00, 0x08, 0x80, - 0x00, 0x00, 0xE4, 0x90, 0x03, 0x00, 0xE4, 0xA0, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x0F, 0xE0, - 0x14, 0x00, 0xE4, 0x80, 0x17, 0x00, 0x00, 0x03, 0x04, 0x00, 0x07, 0x80, 0x05, 0x00, 0xE4, 0x90, - 0x04, 0x00, 0xE4, 0xA0, 0x01, 0x00, 0x00, 0x02, 0x02, 0x00, 0x0F, 0x80, 0x10, 0x00, 0xE4, 0xA1, - 0x08, 0x00, 0x00, 0x03, 0x03, 0x00, 0x0F, 0x80, 0x04, 0x00, 0xE4, 0x80, 0x02, 0x00, 0xE4, 0x80, - 0x0B, 0x00, 0x00, 0x03, 0x03, 0x00, 0x0F, 0x80, 0x03, 0x00, 0xE4, 0x80, 0x80, 0x00, 0x00, 0xA0, - 0x05, 0x00, 0x00, 0x03, 0x03, 0x00, 0x0F, 0x80, 0x03, 0x00, 0xE4, 0x80, 0x11, 0x00, 0x00, 0xA0, - 0x02, 0x00, 0x00, 0x03, 0x03, 0x00, 0x0F, 0x80, 0x03, 0x00, 0xE4, 0x80, 0x11, 0x00, 0x55, 0xA0, - 0x0A, 0x00, 0x00, 0x03, 0x03, 0x00, 0x0F, 0x80, 0x03, 0x00, 0xE4, 0x80, 0x80, 0x00, 0x55, 0xA0, - 0x29, 0x00, 0x05, 0x02, 0x06, 0x00, 0x00, 0x90, 0x80, 0x00, 0x00, 0xA0, 0x01, 0x00, 0x00, 0x02, - 0x03, 0x00, 0x07, 0x80, 0x06, 0x00, 0x00, 0x90, 0x2B, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x02, - 0x03, 0x00, 0x08, 0x80, 0x80, 0x00, 0xFF, 0xA0, 0x05, 0x00, 0x00, 0x03, 0x01, 0x00, 0x0F, 0xE0, - 0x01, 0x00, 0xE4, 0x90, 0x03, 0x00, 0xE4, 0x80, 0x01, 0x00, 0x00, 0x02, 0x0A, 0x00, 0x0F, 0x80, - 0x02, 0x00, 0xE4, 0x90, 0x01, 0x00, 0x00, 0x02, 0x0A, 0x00, 0x08, 0x80, 0x14, 0x00, 0xFF, 0x80, - 0x01, 0x00, 0x00, 0x02, 0x02, 0x00, 0x0F, 0xE0, 0x0A, 0x00, 0xE4, 0x80, 0x01, 0x00, 0x00, 0x02, - 0x03, 0x00, 0x0F, 0xE0, 0x03, 0x00, 0xE4, 0x90, 0x01, 0x00, 0x00, 0x02, 0x04, 0x00, 0x0F, 0xE0, - 0x04, 0x00, 0xE4, 0x90, 0x01, 0x00, 0x00, 0x02, 0x05, 0x00, 0x01, 0xE0, 0x06, 0x00, 0x55, 0x90, - 0xFF, 0xFF, 0x00, 0x00, -}; - -unsigned char vertex_shader_2d_source[80] = -{ - 0x00, 0x03, 0xFE, 0xFF, 0x1F, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x0F, 0x90, - 0x1F, 0x00, 0x00, 0x02, 0x05, 0x00, 0x00, 0x80, 0x01, 0x00, 0x0F, 0x90, 0x1F, 0x00, 0x00, 0x02, - 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x0F, 0xE0, 0x1F, 0x00, 0x00, 0x02, 0x05, 0x00, 0x00, 0x80, - 0x01, 0x00, 0x0F, 0xE0, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x0F, 0xE0, 0x00, 0x00, 0xE4, 0x90, - 0x01, 0x00, 0x00, 0x02, 0x01, 0x00, 0x0F, 0xE0, 0x01, 0x00, 0xE4, 0x90, 0xFF, 0xFF, 0x00, 0x00, -}; \ No newline at end of file diff --git a/win32/vertex_shader.vs b/win32/vertex_shader.vs deleted file mode 100644 index 083187c..0000000 --- a/win32/vertex_shader.vs +++ /dev/null @@ -1,71 +0,0 @@ -vs_3_0 - -; parameters -; c0...c3 world-view-projection matrix -; c4...c7 world-view matrix -; c16 sun light vector -; c17 sun ambient, diffuse intensity - -; input declarations -dcl_position v0 -dcl_color0 v1 -dcl_texcoord0 v2 -dcl_texcoord1 v3 -dcl_texcoord2 v4 -dcl_normal v5 -dcl_color1 v6 - -; output declarations -dcl_position o0.xyzw -dcl_color0 o1.xyzw -dcl_texcoord0 o2.xyzw -dcl_texcoord1 o3.xyzw -dcl_texcoord2 o4.xyzw -dcl_color1 o5.xyzw - -; constants -def c128, 0.0f, 1.0f, 0.5f, 1.0f - -; transform position to projection space -;dp4 o0.x, v0, c0 -;dp4 o0.y, v0, c1 -;dp4 o0.z, v0, c2 -;dp4 o0.w, v0, c3 -dp4 r20.x, v0, c0 -dp4 r20.y, v0, c1 -dp4 r20.z, v0, c2 -dp4 r20.w, v0, c3 - -mov o0, r20 - -; calculate parallel lighting -m3x3 r4.xyz, v5, c4 ; transform N - -mov r2, -c16 -dp3 r3, r4, r2 ; N dot L -max r3, r3, c128.xxxx ; clamp negative values to 0 - -mul r3, r3, c17.xxxx ; scale with diffuse intensity -add r3, r3, c17.yyyy ; add ambient intensity - -min r3, r3, c128.yyyy - -if_ne v6.xxxx, c128.xxxx ; if v6.x == 0 -> apply lighting, if v6.x != 0 -> self-luminance -mov r3.xyz, v6.xxx ; self-luminance -endif - -mov r3.w, c128.w ; multiply alpha with 1 - -mul o1, v1, r3 ; modulate and write color -;mov o1, r3 - -; move texture parameters -mov r10,v2 -mov r10.w, r20.w -mov o2, r10 -;mov o2, v2 -mov o3, v3 -mov o4, v4 - -; parameters (X=texture enable) -mov o5.x, v6.g \ No newline at end of file diff --git a/win32/vertex_shader_2d.vs b/win32/vertex_shader_2d.vs deleted file mode 100644 index 1bd70da..0000000 --- a/win32/vertex_shader_2d.vs +++ /dev/null @@ -1,14 +0,0 @@ -vs_3_0 - -; input declarations -dcl_position v0 -dcl_texcoord0 v1 - -; output declarations -dcl_position o0.xyzw -dcl_texcoord0 o1.xyzw - - - -mov o0, v0 -mov o1, v1 \ No newline at end of file diff --git a/win32/win_gl.c b/win32/win_gl.c deleted file mode 100644 index eb1bc1a..0000000 --- a/win32/win_gl.c +++ /dev/null @@ -1,181 +0,0 @@ -/* - * Sega Model 3 Emulator - * Copyright (C) 2003 Bart Trzynadlowski, Ville Linde, Stefano Teso - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License Version 2 as published - * by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program (license.txt); if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/* - * win32/win_gl.c - * - * Win32 OpenGL support. All of this will have to be rewritten to support - * run-time mode changes and fullscreen modes. - * - * The only osd_renderer function implemented here is osd_renderer_blit(). - */ - -#include "model3.h" -#include "osd_common/osd_gl.h" -#include -#include -#include - -/* - * Window Context - */ - -static HGLRC hrc = 0; // OpenGL rendering context -static HDC hdc = 0; // GDI device context -static HINSTANCE hinstance; // application instance -extern HWND main_window; // window handle - -/* - * void win_gl_init(UINT xres, UINT yres); - * - * Sets the rendering mode and initializes OpenGL. - * - * Parameters: - * xres = Horizontal resolution in pixels. - * yres = Vertical resolution. - */ - -void win_gl_init(UINT xres, UINT yres) -{ - GLuint pixel_format; - static PIXELFORMATDESCRIPTOR pfd = // must this be static? - { - sizeof(PIXELFORMATDESCRIPTOR), // size of structure - 1, // version (must be 1) - PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, - PFD_TYPE_RGBA, // RGBA pixels - 32, // color depth - 0, 0, 0, 0, 0, 0, // color bits ignored - 0, // no alpha buffer - 0, // ignore shift bit - 0, // no accumulation buffer - 0, 0, 0, 0, // accumulation bits ignored - 24, // 24-bit z-buffer - 0, // no stencil buffer - 0, // no auxilliary buffer - PFD_MAIN_PLANE, // main drawing layer - 0, // reserved - 0, 0, 0 // layer masks ignored - }; - - /* - * Get window instance - */ - - hinstance = GetModuleHandle(NULL); // get window instance - - /* - * Get device context and find a pixel format - */ - - if (!(hdc = GetDC(main_window))) - osd_error("Unable to create an OpenGL device context."); - - if (!(pixel_format = ChoosePixelFormat(hdc, &pfd))) // find matching pixel format - osd_error("Unable to find the required pixel format."); - - if (!SetPixelFormat(hdc, pixel_format, &pfd)) - osd_error("Unable to set the required pixel format."); - - /* - * Get the rendering context and perform some GL initialization - */ - - if (!(hrc = wglCreateContext(hdc))) - osd_error("Unable to create an OpenGL rendering context."); - - if (!wglMakeCurrent(hdc, hrc)) - osd_error("Unable to activate the OpenGL rendering context."); - - /* - * Check for mirrored texture repeat extension - */ - - if (osd_gl_check_extension("GL_ARB_texture_mirrored_repeat")) - osd_error("Your OpenGL implementation does not support mirrored texture repeating!"); - if (osd_gl_check_extension("GL_ARB_texture_env_combine")) - osd_error("Your OpenGL implementation does not support texture combiner operations!"); - if (osd_gl_check_extension("GL_EXT_texture_lod_bias")) - osd_error("Your OpenGL implementation does not support texture LOD bias selection!"); - if (osd_gl_check_extension("GL_ARB_vertex_buffer_object")) - osd_error("Your OpenGL implementation does not support vertex buffer objects!"); - - /* - * Initialize GL engine - */ - - osd_gl_set_mode(xres, yres); -} - -/* - * void win_gl_shutdown(void); - * - * Shuts down the rendering mode mode meaning it releases the rendering and - * device contexts. - */ - -void win_gl_shutdown(void) -{ - osd_gl_unset_mode(); - - /* - * If there is a rendering context, release it - */ - - if (hrc) - { - wglMakeCurrent(NULL, NULL); - wglDeleteContext(hrc); - } - - /* - * If there is a device context, release it - */ - - if (hdc) - ReleaseDC(main_window, hdc); -} - -/* - * void osd_renderer_blit(void); - * - * Swaps the buffers to display what has been rendered in the last frame. - */ - -void osd_renderer_blit(void) -{ - SwapBuffers(hdc); -} - -/* - * void * osd_gl_get_proc_address(const char *); - * - * Calls wglGetProcAddress. - */ - -void * osd_gl_get_proc_address(const CHAR * id) -{ - void * ptr = wglGetProcAddress(id); - - if (ptr == NULL) - error("GL proc %s not found!\n", id); - else - message(0, "found GL proc %s!", id); - - return ptr; -} diff --git a/win32/win_gl.h b/win32/win_gl.h deleted file mode 100644 index efa8949..0000000 --- a/win32/win_gl.h +++ /dev/null @@ -1,13 +0,0 @@ -/* - * win32/win_gl.h - * - * Windows OpenGL header. - */ - -#ifndef INCLUDED_WIN32_WIN_GL_H -#define INCLUDED_WIN32_WIN_GL_H - -extern void win_gl_init(UINT, UINT); -extern void win_gl_shutdown(void); - -#endif // INCLUDED_WIN32_WIN_GL_H diff --git a/win32/win_input.c b/win32/win_input.c deleted file mode 100644 index 1c7c031..0000000 --- a/win32/win_input.c +++ /dev/null @@ -1,319 +0,0 @@ -/* - * Sega Model 3 Emulator - * Copyright (C) 2003 Bart Trzynadlowski, Ville Linde, Stefano Teso - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License Version 2 as published - * by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program (license.txt); if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/******************************************************************/ -/* DirectX 9 Input */ -/******************************************************************/ - -#include "MODEL3.H" -#include - -static LPDIRECTINPUT8 dinput; -static LPDIRECTINPUTDEVICE8 keyboard; -static LPDIRECTINPUTDEVICE8 mouse; - -static CHAR keyboard_buffer[256]; -static DIMOUSESTATE mouse_state; - -extern HWND main_window; - -static OSD_CONTROLS controls; - -void osd_input_init(void) -{ - HINSTANCE hinstance; - HRESULT hr; - - atexit(osd_input_shutdown); - - hinstance = GetModuleHandle(NULL); - hr = DirectInput8Create( hinstance, DIRECTINPUT_VERSION, &IID_IDirectInput8, - (void**)&dinput, NULL ); - if(FAILED(hr)) - error("DirectInput8Create failed."); - - // Create keyboard device - hr = IDirectInput8_CreateDevice( dinput, &GUID_SysKeyboard, &keyboard, NULL ); - if(FAILED(hr)) - error("IDirectInput8_CreateDevice failed."); - - hr = IDirectInputDevice8_SetDataFormat( keyboard, &c_dfDIKeyboard ); - if(FAILED(hr)) - error("IDirectInputDevice8_SetDataFormat failed."); - - hr = IDirectInputDevice8_SetCooperativeLevel( keyboard, main_window, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE ); - if(FAILED(hr)) - error("IDirectInputDevice8_SetCooperativeLevel failed."); - - if(keyboard) - IDirectInputDevice8_Acquire( keyboard ); - - // Create mouse device - hr = IDirectInput8_CreateDevice( dinput, &GUID_SysMouse, &mouse, NULL ); - if(FAILED(hr)) - error("IDirectInput8_CreateDevice failed."); - - hr = IDirectInputDevice8_SetDataFormat( mouse, &c_dfDIMouse ); - if(FAILED(hr)) - error("IDirectInputDevice8_SetDataFormat failed."); - - hr = IDirectInputDevice8_SetCooperativeLevel( mouse, main_window, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE ); - if(FAILED(hr)) - error("IDirectInputDevice8_SetCooperativeLevel failed."); -} - -void osd_input_shutdown(void) -{ - if(dinput) { - if(keyboard) { - IDirectInputDevice8_Unacquire( keyboard ); - IDirectInputDevice8_Release( keyboard ); - keyboard = NULL; - } - if(mouse) { - IDirectInputDevice8_Unacquire( mouse ); - IDirectInputDevice8_Release( mouse ); - mouse = NULL; - } - IDirectInput8_Release( dinput ); - dinput = NULL; - } -} - -static void input_update(void) -{ - /* updates the input buffer */ - if(dinput) { - // Get keyboard state - IDirectInputDevice8_GetDeviceState( keyboard, sizeof(keyboard_buffer), &keyboard_buffer ); - // Get mouse state - IDirectInputDevice8_Acquire( mouse ); - IDirectInputDevice8_GetDeviceState( mouse, sizeof(mouse_state), &mouse_state ); - } -} - -static BOOL keyboard_get_key(UINT8 key) -{ - if(keyboard_buffer[key] & 0x80) - return TRUE; - else - return FALSE; -} - -static BOOL mouse_get_button(UINT8 button) -{ - if(mouse_state.rgbButtons[button] & 0x80) - return TRUE; - else - return FALSE; -} - -static void mouse_get_position(INT32* xposition, INT32* yposition) -{ - POINT mouse_pos; - - GetCursorPos( &mouse_pos ); - ScreenToClient( main_window, &mouse_pos ); - - *xposition = mouse_pos.x + (400 - (MODEL3_SCREEN_WIDTH / 2)); - *yposition = mouse_pos.y + (272 - (MODEL3_SCREEN_HEIGHT / 2)); -} - -OSD_CONTROLS* osd_input_update_controls(void) -{ - INT32 mouse_x, mouse_y; - input_update(); - - controls.game_controls[0] = 0xFF; - controls.game_controls[1] = 0xFF; - controls.system_controls[0] = 0xFF; - controls.system_controls[1] = 0xFF; - - // Lightgun - if(mouse_get_button(0)) - controls.game_controls[0] &= ~0x01; - - if(mouse_get_button(1)) - controls.gun_acquired[0] = TRUE; - else - controls.gun_acquired[0] = FALSE; - - mouse_get_position(&mouse_x, &mouse_y); - controls.gun_x[0] = mouse_x; - controls.gun_y[0] = mouse_y; - - // Game controls - if(keyboard_get_key(DIK_A)) - controls.game_controls[0] &= ~0x01; - if(keyboard_get_key(DIK_S)) - controls.game_controls[0] &= ~0x02; - if(keyboard_get_key(DIK_D)) - controls.game_controls[0] &= ~0x04; - if(keyboard_get_key(DIK_F)) - controls.game_controls[0] &= ~0x08; - if(keyboard_get_key(DIK_G)) - controls.game_controls[0] &= ~0x80; - if(keyboard_get_key(DIK_H)) - controls.game_controls[0] &= ~0x40; - - if(keyboard_get_key(DIK_Q)) - controls.game_controls[0] &= ~0x80; - if(keyboard_get_key(DIK_W)) - controls.game_controls[0] &= ~0x40; - if(keyboard_get_key(DIK_E)) - controls.game_controls[0] &= ~0x20; - if(keyboard_get_key(DIK_R)) - controls.game_controls[0] &= ~0x10; - if(keyboard_get_key(DIK_T)) - controls.game_controls[1] &= ~0x80; - if(keyboard_get_key(DIK_Y)) - controls.game_controls[1] &= ~0x40; - if(keyboard_get_key(DIK_U)) - controls.game_controls[1] &= ~0x20; - if(keyboard_get_key(DIK_I)) - controls.game_controls[1] &= ~0x10; - - if(keyboard_get_key(DIK_Z)) // VON2, shot trigger 1 - controls.game_controls[0] &= ~0x01; - if(keyboard_get_key(DIK_X)) // VON2, shot trigger 2 - controls.game_controls[1] &= ~0x01; - if(keyboard_get_key(DIK_C)) // VON2, turbo 1 - controls.game_controls[0] &= ~0x02; - if(keyboard_get_key(DIK_V)) // VON2, turbo 2 - controls.game_controls[1] &= ~0x02; - - /*if(keyboard_get_key(DIK_UP) || keyboard_get_key(DIK_NUMPAD8)) { - controls.game_controls[0] &= ~0x20; // VON2, forward - controls.game_controls[1] &= ~0x20; - } - if(keyboard_get_key(DIK_DOWN) || keyboard_get_key(DIK_NUMPAD2)) { - controls.game_controls[0] &= ~0x10; // VON2, backward - controls.game_controls[1] &= ~0x10; - } - if(keyboard_get_key(DIK_LEFT) || keyboard_get_key(DIK_NUMPAD4)) { - controls.game_controls[0] &= ~0x10; // VON2, turn left - controls.game_controls[1] &= ~0x20; - } - if(keyboard_get_key(DIK_RIGHT) || keyboard_get_key(DIK_NUMPAD6)) { - controls.game_controls[0] &= ~0x20; // VON2, turn right - controls.game_controls[1] &= ~0x10; - }*/ - if(keyboard_get_key(DIK_NUMPAD1)) {// VON2, strafe left - controls.game_controls[0] &= ~0x80; - controls.game_controls[1] &= ~0x80; - } - if(keyboard_get_key(DIK_NUMPAD3)) {// VON2, strafe right - controls.game_controls[0] &= ~0x40; - controls.game_controls[1] &= ~0x40; - } - if(keyboard_get_key(DIK_NUMPAD5)) {// VON2, jump - controls.game_controls[0] &= ~0x80; - controls.game_controls[1] &= ~0x40; - } - - // System controls - if(keyboard_get_key(DIK_F1)) { // Test button - controls.system_controls[0] &= ~0x04; - controls.system_controls[1] &= ~0x04; - } - if(keyboard_get_key(DIK_F2)) { // Service button - controls.system_controls[0] &= ~0x08; - controls.system_controls[1] &= ~0x80; - } - if(keyboard_get_key(DIK_F3)) { // Start button - controls.system_controls[0] &= ~0x10; - } - if(keyboard_get_key(DIK_F4)) { // Coin #1 - controls.system_controls[0] &= ~0x01; - } - - // Steering Wheel -#if 0 - if(keyboard_get_key(DIK_LEFT)) - controls.steering -= 32; - if(keyboard_get_key(DIK_RIGHT)) - controls.steering += 32; - - if(controls.steering > 0) { - controls.steering -= 16; - if(controls.steering < 0) - controls.steering = 0; - } else { - controls.steering += 16; - if(controls.steering > 0) - controls.steering = 0; - } - - if(controls.steering < -128) - controls.steering = -128; - if(controls.steering > 127) - controls.steering = 127; - - if(keyboard_get_key(DIK_UP)) - controls.acceleration += 32; - else - controls.acceleration -= 16; - - if(controls.acceleration < 0) - controls.acceleration = 0; - if(controls.acceleration > 0xFF) - controls.acceleration = 0xFF; - - if(keyboard_get_key(DIK_DOWN)) - controls.brake += 32; - else - controls.brake -= 16; - - if(controls.brake < 0) - controls.brake = 0; - if(controls.brake > 0xFF) - controls.brake = 0xFF; -#else - - /* SWT Force Feedback joystick */ - if(keyboard_get_key(DIK_UP)) - controls.steering -= 16; - if(keyboard_get_key(DIK_DOWN)) - controls.steering += 16; - - if(controls.steering < -128) - controls.steering = -128; - if(controls.steering > 127) - controls.steering = 127; - - if(keyboard_get_key(DIK_RIGHT)) { - controls.acceleration += 16; - controls.brake -= 16; - } - if(keyboard_get_key(DIK_LEFT)) { - controls.brake += 16; - controls.acceleration -= 16; - } - - if(controls.acceleration < 0) - controls.acceleration = 0; - if(controls.acceleration > 0xFF) - controls.acceleration = 0xFF; - if(controls.brake < 0) - controls.brake = 0; - if(controls.brake > 0xFF) - controls.brake = 0xFF; -#endif - - return &controls; -} diff --git a/win32/win_main.c b/win32/win_main.c deleted file mode 100644 index bc74601..0000000 --- a/win32/win_main.c +++ /dev/null @@ -1,477 +0,0 @@ -/* - * Sega Model 3 Emulator - * Copyright (C) 2003 Bart Trzynadlowski, Ville Linde, Stefano Teso - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License Version 2 as published - * by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program (license.txt); if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/******************************************************************/ -/* Windows Main */ -/******************************************************************/ - -#include "model3.h" -#ifdef RENDERER_D3D -#include "dx_render.h" -#else // RENDERER_GL -#include "win_gl.h" -#endif - -#define XRES (496) -#define YRES (384) - -static CHAR app_title[] = "Supermodel"; -static CHAR app_version[] = "1.0"; -static CHAR class_name[] = "MODEL3"; - -static CHAR CONFIG_FILE[] = "config.xml"; - -HWND main_window; - -// Window Procedure prototype -static LRESULT CALLBACK win_window_proc(HWND, UINT, WPARAM, LPARAM); - -static BOOL win_register_class(void) -{ - WNDCLASSEX wcex; - - wcex.cbSize = sizeof(WNDCLASSEX); - wcex.style = CS_HREDRAW | CS_VREDRAW; - wcex.lpfnWndProc = (WNDPROC)win_window_proc; - wcex.cbClsExtra = 0; - wcex.cbWndExtra = 0; - wcex.hInstance = GetModuleHandle(NULL); - wcex.hIcon = NULL; - wcex.hIconSm = NULL; - wcex.hCursor = LoadCursor(NULL, IDC_ARROW); - wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); - wcex.lpszMenuName = NULL; - wcex.lpszClassName = class_name; - - if (FAILED(RegisterClassEx(&wcex))) // MinGW: "comparison is always false due to limited range of data" - return FALSE; - - return TRUE; -} - -static BOOL win_create_window(UINT xres, UINT yres) -{ - DWORD frame_width, frame_height, caption_height; - int width, height, window_width, window_height; - - window_width = xres; - window_height = yres; - - frame_width = GetSystemMetrics(SM_CXSIZEFRAME); - frame_height = GetSystemMetrics(SM_CYSIZEFRAME); - caption_height = GetSystemMetrics(SM_CYCAPTION); - - width = (window_width - 1) + (frame_width * 2); - height = (window_height - 1) + (frame_height * 2) + caption_height; - - main_window = CreateWindow(class_name, - app_title, - WS_CLIPSIBLINGS | WS_CLIPCHILDREN | // required for OpenGL - WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX, - CW_USEDEFAULT, CW_USEDEFAULT, // Window X & Y coords - width - 1, height - 1, // Width & Height - NULL, NULL, // Parent Window & Menu - GetModuleHandle(NULL), NULL ); - - if (!main_window) - return FALSE; - - return TRUE; -} - -/* - * NOTE: This should actually return an error if something goes wrong, but - * it doesn't matter now. This stuff probably needs to be rewritten anyway ;) - */ -static void win_destroy(void) -{ -#ifdef RENDERER_D3D - d3d_shutdown(); -#else // RENDERER_GL - win_gl_shutdown(); -#endif - DestroyWindow(main_window); - UnregisterClass(class_name, GetModuleHandle(NULL)); -} - -void *malloc_exec(int length) -{ - void *ptr; - ptr = VirtualAlloc(NULL, length, MEM_COMMIT, PAGE_EXECUTE_READWRITE); - - if (ptr == NULL) - { - error("malloc_exec %d failed\n", length); - } - - return ptr; -} - -void free_exec(void *ptr) -{ - if (VirtualFree(ptr, 0, MEM_RELEASE) == FALSE) - { - error("free_exec failed\n"); - } -} - -BOOL check_cpu_features(void) -{ - BOOL features_ok = TRUE; - char cpuname[256]; - UINT32 cpu_version; - UINT32 cpu_features; - - memset(cpuname, 0, sizeof(cpuname)); - - __asm - { - // get cpu name string - mov eax, 0x80000002 - cpuid - mov dword ptr [cpuname+0 ], eax - mov dword ptr [cpuname+4 ], ebx - mov dword ptr [cpuname+8 ], ecx - mov dword ptr [cpuname+12 ], edx - mov eax, 0x80000003 - cpuid - mov dword ptr [cpuname+16 ], eax - mov dword ptr [cpuname+20 ], ebx - mov dword ptr [cpuname+24 ], ecx - mov dword ptr [cpuname+28 ], edx - mov eax, 0x80000004 - cpuid - mov dword ptr [cpuname+32 ], eax - mov dword ptr [cpuname+36 ], ebx - mov dword ptr [cpuname+40 ], ecx - mov dword ptr [cpuname+44 ], edx - - // get cpu version and features - mov eax, 1 - cpuid - mov [cpu_version], eax - mov [cpu_features], edx - } - message(0, "CPU: %s", cpuname); - if ((cpu_features & (1 << 15)) == 0) - { - message(0, "CPU doesn't support Conditional Move/Compare instructions"); - features_ok = FALSE; - } - if ((cpu_features & (1 << 23)) == 0) - { - message(0, "CPU doesn't support MMX instructions"); - features_ok = FALSE; - } - if ((cpu_features & (1 << 25)) == 0) - { - message(0, "CPU doesn't support SSE instructions"); - features_ok = FALSE; - } - if ((cpu_features & (1 << 26)) == 0) - { - message(0, "CPU doesn't support SSE2 instructions"); - features_ok = FALSE; - } - - if (features_ok == FALSE) - { - message(0, "The CPU doesn't meet the requirements, the program will not run further."); - } - return features_ok; -} - -int main(int argc, char *argv[]) -{ - MSG msg; - BOOL quit = FALSE, do_fps; - INT64 freq, time_start, time_end; - char title[256]; - double fps = 0.0; - int frame = 0; - - if(argc < 2) { - // Show usage - printf("ERROR: not enough arguments.\n\n"); - printf("Usage: m3.exe [romset]\n"); - return 0; - } - - message(0, "%s v%s\n", app_title, app_version); - - if (check_cpu_features() == FALSE) - { - exit(1); - } - -#ifdef RENDERER_D3D - if (d3d_pre_init() == FALSE) - { - message(0, "The video card doesn't meet the requirements, the program will not run further."); - exit(1); - } -#endif - - message(0, ""); - - // Load config - - if (parse_config(CONFIG_FILE) == FALSE) - { - exit(1); - } - m3_config.layer_enable = 0xF; - - // Parse command-line - - strncpy(m3_config.game_id, argv[1], 8); - m3_config.game_id[8] = '\0'; // in case game name was 8 or more chars - - if (stricmp(m3_config.game_id, "lostwsga") == 0) - { - m3_config.has_lightgun = TRUE; - } - - // Some initialization - - if (!win_register_class()) - { - message(0, "win_register_class failed."); - exit(1); - } - if (!win_create_window(XRES, YRES)) - { - message(0, "win_create_window failed."); - exit(1); - } - - if (model3_load() == FALSE) - { - message(0, "ROM loading failed"); - exit(1); - } - model3_init(); - model3_reset(); - -#ifdef RENDERER_D3D - if (!d3d_init(main_window)) - { - message(0, "d3d_init failed."); - exit(1); - } -#else - win_gl_init(XRES, YRES); -#endif - - if (osd_input_init() == FALSE) - { - exit(1); - } - - // Set window title to show the game name - sprintf(title, "%s: %s", app_title, m3_config.game_name); - SetWindowText(main_window, title); - - // Now that everything works, we can show the window - - ShowWindow(main_window, SW_SHOWNORMAL); - SetForegroundWindow(main_window); - SetFocus(main_window); - UpdateWindow(main_window); - - if (m3_config.fullscreen && !m3_config.has_lightgun) - { - ShowCursor(FALSE); - } - - if(QueryPerformanceFrequency((LARGE_INTEGER *)&freq)) - do_fps = TRUE; - else - do_fps = FALSE; - - QueryPerformanceCounter((LARGE_INTEGER *)&time_start); - QueryPerformanceCounter((LARGE_INTEGER *)&time_end); - - memset(&msg, 0, sizeof(MSG)); - while (quit == FALSE) - { - //QueryPerformanceCounter((LARGE_INTEGER *)&time_start); - - while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) - { - if (msg.message == WM_QUIT) - quit = TRUE; - else - { - TranslateMessage(&msg); - DispatchMessage(&msg); - } - } - - model3_run_frame(); - frame++; - - // gather profiler stats - - //QueryPerformanceCounter((LARGE_INTEGER *)&time_end); - if (frame >= 5) - { - frame = 0; - QueryPerformanceCounter((LARGE_INTEGER *)&time_end); - fps = 5.0 / ((double)(time_end - time_start) / freq); - - time_start = time_end; - } - -#ifdef RENDERER_D3D - if (m3_config.show_fps) - { - //fps = 1.0 / ((double)(time_end - time_start) / freq); - sprintf(title, "FPS: %.3f", fps); - osd_renderer_draw_text(2, 2, title, 0xffff0000, TRUE); - } -#else - sprintf(title, "%s: %s, FPS: %.3f", app_title, m3_config.game_name, fps); - SetWindowText(main_window, title); -#endif - - //osd_renderer_draw_text(2, 2, title, 0x00ff0000, TRUE); - - osd_renderer_blit(); - -#ifdef _PROFILE_ - profile_print(prof); - - printf(prof); -#endif - } - - //for (i = 0; i < 32; i += 4) - // printf("R%d=%08X\tR%d=%08X\tR%d=%08X\tR%d=%08X\n", - // i + 0, ppc_get_reg(PPC_REG_R0 + i + 0), - // i + 1, ppc_get_reg(PPC_REG_R0 + i + 1), - // i + 2, ppc_get_reg(PPC_REG_R0 + i + 2), - // i + 3, ppc_get_reg(PPC_REG_R0 + i + 3)); - exit(0); -} - -void osd_warning() -{ - -} - -void osd_error(CHAR * string) -{ - printf("ERROR: %s\n",string); - exit(0); -} - -static LRESULT CALLBACK win_window_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) -{ - CHAR fname[13]; - static UINT xres = 496, yres = 384; - - switch(message) - { - case WM_DESTROY: - { - PostQuitMessage(0); - break; - } - - case WM_KEYDOWN: - { - switch (wParam) - { - default: - break; - - case '7': - m3_config.layer_enable ^= 1; - break; - case '8': - m3_config.layer_enable ^= 2; - break; - case '9': - m3_config.layer_enable ^= 4; - break; - case '0': - m3_config.layer_enable ^= 8; - break; - case 0x36: - m3_config.layer_enable = 0xF; - break; - case VK_ESCAPE: - DestroyWindow(hWnd); - break; - /*case VK_F7: - strncpy(fname, m3_config.game_id, 8); - fname[8] = '\0'; - strcat(fname, ".sta"); - model3_save_state(fname); - break; - case VK_F8: - strncpy(fname, m3_config.game_id, 8); - fname[8] = '\0'; - strcat(fname, ".sta"); - model3_load_state(fname); - break;*/ - } - break; - } - - case WM_KEYUP: - { - switch (wParam) - { - default: - break; - - case VK_F11: - { - if (m3_config.fps_limit) - { - m3_config.fps_limit = FALSE; - } - else - { - m3_config.fps_limit = TRUE; - } - break; - } - case VK_F12: - { - if (m3_config.show_fps) - { - m3_config.show_fps = FALSE; - } - else - { - m3_config.show_fps = TRUE; - } - break; - } - break; - } - } - - default: - return DefWindowProc(hWnd, message, wParam, lParam); - } - return 0; -} diff --git a/win32/win_xinput.c b/win32/win_xinput.c deleted file mode 100644 index 57f5182..0000000 --- a/win32/win_xinput.c +++ /dev/null @@ -1,463 +0,0 @@ -/* - * Sega Model 3 Emulator - * Copyright (C) 2003 Bart Trzynadlowski, Ville Linde, Stefano Teso - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License Version 2 as published - * by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program (license.txt); if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/******************************************************************/ -/* XInput for Xbox360 controller */ -/******************************************************************/ - -#include "model3.h" -#include -#include - -static LPDIRECTINPUT8 dinput; -static LPDIRECTINPUTDEVICE8 keyboard; -static LPDIRECTINPUTDEVICE8 mouse; - -static CHAR keyboard_buffer[256]; -static DIMOUSESTATE mouse_state; - -extern HWND main_window; - -static OSD_CONTROLS controls; - -static int xbox_controllers[4]; - - -static int analog_axis[8]; -static BOOL button_state[16]; - -static struct -{ - BOOL up, down, left, right; -} joystick[4]; - -BOOL osd_input_init(void) -{ - HINSTANCE hinstance; - HRESULT hr; - - DWORD result; - XINPUT_STATE state; - - atexit(osd_input_shutdown); - - hinstance = GetModuleHandle(NULL); - hr = DirectInput8Create( hinstance, DIRECTINPUT_VERSION, &IID_IDirectInput8, - (void**)&dinput, NULL ); - if (FAILED(hr)) - { - message(0, "DirectInput8Create failed."); - return FALSE; - } - - // Create keyboard device - hr = IDirectInput8_CreateDevice( dinput, &GUID_SysKeyboard, &keyboard, NULL ); - if (FAILED(hr)) - { - message(0, "IDirectInput8_CreateDevice failed."); - return FALSE; - } - - hr = IDirectInputDevice8_SetDataFormat( keyboard, &c_dfDIKeyboard ); - if (FAILED(hr)) - { - message(0, "IDirectInputDevice8_SetDataFormat failed."); - return FALSE; - } - - hr = IDirectInputDevice8_SetCooperativeLevel( keyboard, main_window, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE ); - if (FAILED(hr)) - { - message(0, "IDirectInputDevice8_SetCooperativeLevel failed."); - return FALSE; - } - - if (keyboard) - { - IDirectInputDevice8_Acquire( keyboard ); - } - - // Create mouse device - hr = IDirectInput8_CreateDevice( dinput, &GUID_SysMouse, &mouse, NULL ); - if (FAILED(hr)) - { - message(0, "IDirectInput8_CreateDevice failed."); - return FALSE; - } - - hr = IDirectInputDevice8_SetDataFormat( mouse, &c_dfDIMouse ); - if (FAILED(hr)) - { - message(0, "IDirectInputDevice8_SetDataFormat failed."); - return FALSE; - } - - hr = IDirectInputDevice8_SetCooperativeLevel( mouse, main_window, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE ); - if (FAILED(hr)) - { - message(0, "IDirectInputDevice8_SetCooperativeLevel failed."); - return FALSE; - } - - // Init Xbox360 controllers - ZeroMemory(&state, sizeof(XINPUT_STATE)); - - // Simply get the state of the controller from XInput. - result = XInputGetState(0, &state); - - if (result == ERROR_SUCCESS) - { - // Controller is connected - xbox_controllers[0] = 1; - message(0, "Xbox360 controller found!"); - } - else - { - // Controller is not connected - xbox_controllers[0] = 0; - } - - return TRUE; -} - -void osd_input_shutdown(void) -{ - if(dinput) - { - if(keyboard) - { - IDirectInputDevice8_Unacquire( keyboard ); - IDirectInputDevice8_Release( keyboard ); - keyboard = NULL; - } - if(mouse) - { - IDirectInputDevice8_Unacquire( mouse ); - IDirectInputDevice8_Release( mouse ); - mouse = NULL; - } - IDirectInput8_Release( dinput ); - dinput = NULL; - } -} - -static void input_update(void) -{ - /* updates the input buffer */ - if(dinput) - { - // Get keyboard state - IDirectInputDevice8_GetDeviceState( keyboard, sizeof(keyboard_buffer), &keyboard_buffer ); - // Get mouse state - IDirectInputDevice8_Acquire( mouse ); - IDirectInputDevice8_GetDeviceState( mouse, sizeof(mouse_state), &mouse_state ); - } -} - -static BOOL keyboard_get_key(UINT8 key) -{ - if (keyboard_buffer[key] & 0x80) - { - return TRUE; - } - else - { - return FALSE; - } -} - -static BOOL mouse_get_button(UINT8 button) -{ - if (mouse_state.rgbButtons[button] & 0x80) - { - return TRUE; - } - else - { - return FALSE; - } -} - -static void mouse_get_position(INT32* xposition, INT32* yposition) -{ - POINT mouse_pos; - - GetCursorPos( &mouse_pos ); - ScreenToClient( main_window, &mouse_pos ); - - *xposition = mouse_pos.x; - *yposition = mouse_pos.y; -} - -static void update_controls_keyboard(void) -{ - memset(button_state, 0, sizeof(button_state)); - memset(joystick, 0, sizeof(joystick)); - - if (keyboard_get_key(DIK_A)) button_state[0] = 1; - if (keyboard_get_key(DIK_S)) button_state[1] = 1; - if (keyboard_get_key(DIK_D)) button_state[2] = 1; - if (keyboard_get_key(DIK_F)) button_state[3] = 1; - if (keyboard_get_key(DIK_Z)) button_state[4] = 1; - if (keyboard_get_key(DIK_X)) button_state[5] = 1; - if (keyboard_get_key(DIK_C)) button_state[6] = 1; - if (keyboard_get_key(DIK_V)) button_state[7] = 1; - - if (keyboard_get_key(DIK_LEFT)) joystick[0].left = TRUE; - if (keyboard_get_key(DIK_RIGHT)) joystick[0].right = TRUE; - if (keyboard_get_key(DIK_UP)) joystick[0].up = TRUE; - if (keyboard_get_key(DIK_DOWN)) joystick[0].down = TRUE; - - if (keyboard_get_key(DIK_LEFT)) analog_axis[0] -= 16; - if (keyboard_get_key(DIK_RIGHT)) analog_axis[0] += 16; - if (keyboard_get_key(DIK_UP)) analog_axis[1] -= 16; - if (keyboard_get_key(DIK_DOWN)) analog_axis[1] += 16; - - if (analog_axis[0] < -128) analog_axis[0] = -128; - if (analog_axis[0] > 127) analog_axis[0] = 127; - if (analog_axis[1] < -128) analog_axis[1] = -128; - if (analog_axis[1] > 127) analog_axis[1] = 127; -} - -static void update_controls_xbox(void) -{ - XINPUT_STATE state; - DWORD res; - - memset(button_state, 0, sizeof(button_state)); - memset(analog_axis, 0, sizeof(analog_axis)); - memset(joystick, 0, sizeof(joystick)); - - res = XInputGetState(0, &state); - if (res == ERROR_SUCCESS) - { - // Zero value if thumbsticks are within the dead zone - if ((state.Gamepad.sThumbLX < XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE && - state.Gamepad.sThumbLX > -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE)) - { - state.Gamepad.sThumbLX = 0; - } - if ((state.Gamepad.sThumbLY < XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE && - state.Gamepad.sThumbLY > -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE)) - { - - state.Gamepad.sThumbLY = 0; - } - - if ((state.Gamepad.sThumbRX < XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE && - state.Gamepad.sThumbRX > -XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE)) - { - state.Gamepad.sThumbRX = 0; - } - if ((state.Gamepad.sThumbRY < XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE && - state.Gamepad.sThumbRY > -XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE)) - { - state.Gamepad.sThumbRY = 0; - } - - - if (state.Gamepad.wButtons & XINPUT_GAMEPAD_A) button_state[0] = TRUE; - if (state.Gamepad.wButtons & XINPUT_GAMEPAD_B) button_state[1] = TRUE; - if (state.Gamepad.wButtons & XINPUT_GAMEPAD_X) button_state[2] = TRUE; - if (state.Gamepad.wButtons & XINPUT_GAMEPAD_Y) button_state[3] = TRUE; - - if (state.Gamepad.sThumbLX < 0) joystick[0].left = TRUE; - if (state.Gamepad.sThumbLX > 0) joystick[0].right = TRUE; - if (state.Gamepad.sThumbLY < 0) joystick[0].up = TRUE; - if (state.Gamepad.sThumbLY > 0) joystick[0].down = TRUE; - - analog_axis[0] = state.Gamepad.sThumbLX / 256; - analog_axis[1] = state.Gamepad.sThumbLY / 256; - analog_axis[2] = state.Gamepad.bRightTrigger; - analog_axis[3] = state.Gamepad.bLeftTrigger; - analog_axis[4] = state.Gamepad.sThumbRX; - analog_axis[5] = state.Gamepad.sThumbRY; - } -} - -static int get_analog_axis(GAME_ANALOG axis) -{ - switch (axis) - { - case ANALOG_AXIS_1: return analog_axis[0]; - case ANALOG_AXIS_2: return analog_axis[1]; - case ANALOG_AXIS_3: return analog_axis[2]; - case ANALOG_AXIS_4: return analog_axis[3]; - case ANALOG_AXIS_5: return analog_axis[4]; - case ANALOG_AXIS_6: return analog_axis[5]; - case ANALOG_AXIS_7: return analog_axis[6]; - case ANALOG_AXIS_8: return analog_axis[7]; - } - - return 0; -} - -static BOOL is_button_pressed(GAME_BUTTON button) -{ - switch (button) - { - case P1_BUTTON_1: return button_state[0]; - case P1_BUTTON_2: return button_state[1]; - case P1_BUTTON_3: return button_state[2]; - case P1_BUTTON_4: return button_state[3]; - case P1_BUTTON_5: return button_state[4]; - case P1_BUTTON_6: return button_state[5]; - case P1_BUTTON_7: return button_state[6]; - case P1_BUTTON_8: return button_state[7]; - case P2_BUTTON_1: return button_state[8]; - case P2_BUTTON_2: return button_state[9]; - case P2_BUTTON_3: return button_state[10]; - case P2_BUTTON_4: return button_state[11]; - case P2_BUTTON_5: return button_state[12]; - case P2_BUTTON_6: return button_state[13]; - case P2_BUTTON_7: return button_state[14]; - case P2_BUTTON_8: return button_state[15]; - case P1_JOYSTICK_UP: return joystick[0].up; - case P1_JOYSTICK_DOWN: return joystick[0].down; - case P1_JOYSTICK_LEFT: return joystick[0].left; - case P1_JOYSTICK_RIGHT: return joystick[0].right; - } - - return FALSE; -} - -OSD_CONTROLS* osd_input_update_controls(void) -{ - int i; - INT32 mouse_x, mouse_y; - input_update(); - - controls.game_controls[0] = 0xFF; - controls.game_controls[1] = 0xFF; - controls.system_controls[0] = 0xFF; - controls.system_controls[1] = 0xFF; - - // Lightgun - if(mouse_get_button(0)) - controls.game_controls[0] &= ~0x01; - - if(mouse_get_button(1)) - controls.gun_acquired[0] = TRUE; - else - controls.gun_acquired[0] = FALSE; - - mouse_get_position(&mouse_x, &mouse_y); - if (!m3_config.fullscreen) - { - controls.gun_x[0] = mouse_x + (400 - (MODEL3_SCREEN_WIDTH / 2)); - controls.gun_y[0] = mouse_y + (272 - (MODEL3_SCREEN_HEIGHT / 2)); - } - else - { - if (m3_config.stretch) - { - mouse_x = (mouse_x * 496) / m3_config.width; - mouse_y = (mouse_y * 384) / m3_config.height; - } - - controls.gun_x[0] = mouse_x + (400 - (MODEL3_SCREEN_WIDTH / 2)); - controls.gun_y[0] = mouse_y + (272 - (MODEL3_SCREEN_HEIGHT / 2)); - } - - // Game controls - if (xbox_controllers[0]) - { - update_controls_xbox(); - } - else - { - update_controls_keyboard(); - } - - // update button states - for (i=0; i < 16; i++) - { - if (m3_config.controls.button[i].enabled) - { - if (is_button_pressed(m3_config.controls.button[i].mapping)) - { - int set = m3_config.controls.button[i].control_set; - controls.game_controls[set] &= ~m3_config.controls.button[i].control_bit; - } - } - } - - // update analog controls - for (i=0; i < 8; i++) - { - if (m3_config.controls.analog_axis[i].enabled) - { - int value = get_analog_axis(m3_config.controls.analog_axis[i].mapping); - value += m3_config.controls.analog_axis[i].center; - controls.analog_axis[i] = value; - } - } - - // Lightgun hack for Star Wars Trilogy - if (stricmp(m3_config.game_id, "swtrilgy") == 0) - { - mouse_get_position(&mouse_x, &mouse_y); - - mouse_x = (mouse_x * 256) / ((m3_config.fullscreen) ? m3_config.width : 496); - mouse_y = (mouse_y * 256) / ((m3_config.fullscreen) ? m3_config.height : 384); - - controls.analog_axis[0] = mouse_y; - controls.analog_axis[1] = mouse_x; - - if (mouse_get_button(1)) - controls.game_controls[0] &= ~0x01; - if (mouse_get_button(0)) - controls.game_controls[0] &= ~0x20; - } - - // System controls - if (keyboard_get_key(DIK_F1)) // Service button - { - controls.system_controls[0] &= ~0x08; - } - if (keyboard_get_key(DIK_F2)) // Test button - { - controls.system_controls[0] &= ~0x04; - } - if (keyboard_get_key(DIK_F3)) // Service button B - { - controls.system_controls[1] &= ~0x40; - } - if (keyboard_get_key(DIK_F4)) // Test button B - { - controls.system_controls[1] &= ~0x80; - } - if (keyboard_get_key(DIK_1)) // Start button 1 - { - controls.system_controls[0] &= ~0x10; - } - if (keyboard_get_key(DIK_2)) // Start button 2 - { - controls.system_controls[0] &= ~0x20; - } - if (keyboard_get_key(DIK_5)) // Coin #1 - { - controls.system_controls[0] &= ~0x01; - } - if (keyboard_get_key(DIK_6)) // Coin #2 - { - controls.system_controls[0] &= ~0x02; - } - - return &controls; -}