Last active
September 14, 2024 04:53
-
-
Save YukiSnowy/06ad3adadd38484cb1377ea45152c1a6 to your computer and use it in GitHub Desktop.
example SDL2 Direct3D9 application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// g++ *.cpp -o d3d9 -lmingw32 -lSDL2main -lSDL2 -I/dxsdk/include -L/dxsdk/lib -DUNICODE -ld3d9 -ld3dx9 | |
// http://blog.fourthwoods.com/2011/08/11/setting-up-mingw-for-directx/ | |
// http://www.directxtutorial.com/Lesson.aspx?lessonid=9-4-4 | |
#include <iostream> | |
using namespace std; | |
#include <SDL2/SDL.h> | |
#include <windows.h> | |
#include <d3d9.h> | |
#include <d3dx9.h> | |
IDirect3DDevice9* d3ddev; | |
LPDIRECT3DVERTEXBUFFER9 v_buffer = NULL; | |
struct CUSTOMVERTEX {FLOAT X, Y, Z, RHW; DWORD COLOR;}; | |
#define CUSTOMFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE) | |
void init_graphics(void) | |
{ | |
// create the vertices using the CUSTOMVERTEX struct | |
CUSTOMVERTEX vertices[] = | |
{ | |
{ 400.0f, 62.5f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), }, | |
{ 650.0f, 500.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), }, | |
{ 150.0f, 500.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), }, | |
}; | |
// create a vertex buffer interface called v_buffer | |
d3ddev->CreateVertexBuffer(3*sizeof(CUSTOMVERTEX), | |
0, | |
CUSTOMFVF, | |
D3DPOOL_MANAGED, | |
&v_buffer, | |
NULL); | |
VOID* pVoid; // a void pointer | |
// lock v_buffer and load the vertices into it | |
v_buffer->Lock(0, 0, (void**)&pVoid, 0); | |
memcpy(pVoid, vertices, sizeof(vertices)); | |
v_buffer->Unlock(); | |
} | |
void render_frame(void) | |
{ | |
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0); | |
d3ddev->BeginScene(); | |
// select which vertex format we are using | |
d3ddev->SetFVF(CUSTOMFVF); | |
// select the vertex buffer to display | |
d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX)); | |
// copy the vertex buffer to the back buffer | |
d3ddev->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1); | |
d3ddev->EndScene(); | |
d3ddev->Present(NULL, NULL, NULL, NULL); | |
} | |
int main(int argc, char *argv[]) | |
{ | |
SDL_Window *window; | |
SDL_Renderer *renderer; | |
SDL_Event event; | |
SDL_Init(SDL_INIT_VIDEO); | |
window = SDL_CreateWindow("example SDL2 Direct3D9 application",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,800,600,SDL_WINDOW_RESIZABLE); | |
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); | |
//IDirect3DDevice9* Direct3D_device = SDL_RenderGetD3D9Device(renderer); | |
d3ddev = SDL_RenderGetD3D9Device(renderer); | |
init_graphics(); | |
while (true) | |
{ | |
SDL_PollEvent(&event); | |
if (event.type == SDL_QUIT) | |
{ | |
d3ddev->Release(); | |
break; | |
} | |
render_frame(); | |
} | |
SDL_DestroyRenderer(renderer); | |
SDL_DestroyWindow(window); | |
SDL_Quit(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment