start working on API to draw bitmaps

lay out emu thread
This commit is contained in:
StapleButter 2017-09-14 00:30:12 +02:00
parent 62a26977c7
commit f18690487c
4 changed files with 183 additions and 45 deletions

View File

@ -342,6 +342,8 @@ typedef struct uiDrawMatrix uiDrawMatrix;
typedef struct uiDrawBrushGradientStop uiDrawBrushGradientStop; typedef struct uiDrawBrushGradientStop uiDrawBrushGradientStop;
typedef struct uiDrawBitmap uiDrawBitmap;
_UI_ENUM(uiDrawBrushType) { _UI_ENUM(uiDrawBrushType) {
uiDrawBrushTypeSolid, uiDrawBrushTypeSolid,
uiDrawBrushTypeLinearGradient, uiDrawBrushTypeLinearGradient,
@ -429,6 +431,15 @@ struct uiDrawStrokeParams {
double DashPhase; double DashPhase;
}; };
struct uiRect {
int X;
int Y;
int Width;
int Height;
};
typedef struct uiRect uiRect;
_UI_EXTERN uiDrawPath *uiDrawNewPath(uiDrawFillMode fillMode); _UI_EXTERN uiDrawPath *uiDrawNewPath(uiDrawFillMode fillMode);
_UI_EXTERN void uiDrawFreePath(uiDrawPath *p); _UI_EXTERN void uiDrawFreePath(uiDrawPath *p);
@ -475,6 +486,12 @@ _UI_EXTERN void uiDrawClip(uiDrawContext *c, uiDrawPath *path);
_UI_EXTERN void uiDrawSave(uiDrawContext *c); _UI_EXTERN void uiDrawSave(uiDrawContext *c);
_UI_EXTERN void uiDrawRestore(uiDrawContext *c); _UI_EXTERN void uiDrawRestore(uiDrawContext *c);
// bitmap API
_UI_EXTERN uiDrawBitmap* uiDrawNewBitmap(uiDrawContext* c, int width, int height);
_UI_EXTERN void uiDrawBitmapUpdate(uiDrawBitmap* bmp, const void* data);
_UI_EXTERN void uiDrawBitmapDraw(uiDrawContext* c, uiDrawBitmap* bmp, uiRect* srcrect, uiRect* dstrect);
_UI_EXTERN void uiDrawFreeBitmap(uiDrawBitmap* bmp);
// TODO manage the use of Text, Font, and TextFont, and of the uiDrawText prefix in general // TODO manage the use of Text, Font, and TextFont, and of the uiDrawText prefix in general
///// TODO reconsider this ///// TODO reconsider this

View File

@ -38,7 +38,7 @@ ID2D1HwndRenderTarget *makeHWNDRenderTarget(HWND hwnd)
logLastError(L"error getting DC to find DPI"); logLastError(L"error getting DC to find DPI");
ZeroMemory(&props, sizeof (D2D1_RENDER_TARGET_PROPERTIES)); ZeroMemory(&props, sizeof (D2D1_RENDER_TARGET_PROPERTIES));
props.type = D2D1_RENDER_TARGET_TYPE_DEFAULT; props.type = D2D1_RENDER_TARGET_TYPE_HARDWARE;//DEFAULT;
props.pixelFormat.format = DXGI_FORMAT_UNKNOWN; props.pixelFormat.format = DXGI_FORMAT_UNKNOWN;
props.pixelFormat.alphaMode = D2D1_ALPHA_MODE_UNKNOWN; props.pixelFormat.alphaMode = D2D1_ALPHA_MODE_UNKNOWN;
props.dpiX = GetDeviceCaps(dc, LOGPIXELSX); props.dpiX = GetDeviceCaps(dc, LOGPIXELSX);
@ -509,3 +509,55 @@ void uiDrawRestore(uiDrawContext *c)
// no need to explicitly addref or release; just transfer the ref // no need to explicitly addref or release; just transfer the ref
c->currentClip = state.clip; c->currentClip = state.clip;
} }
// bitmap API
uiDrawBitmap* uiDrawNewBitmap(uiDrawContext* c, int width, int height)
{
uiDrawBitmap* bmp;
HRESULT hr;
bmp = uiNew(uiDrawBitmap);
D2D1_BITMAP_PROPERTIES bp2 = D2D1::BitmapProperties();
bp2.dpiX = 0;
bp2.dpiY = 0;
bp2.pixelFormat = D2D1::PixelFormat(DXGI_FORMAT_R8G8B8A8_UNORM, D2D1_ALPHA_MODE_IGNORE);
//bp2.pixelFormat = D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE);
// TODO: fallback: convert to BGRA if needed (RGBA only works in hardware mode)
c->rt->BeginDraw();
hr = c->rt->CreateBitmap(D2D1::SizeU(width,height), NULL, 0, &bp2, &bmp->bmp);
if (hr != S_OK)
logHRESULT(L"error creating bitmap", hr);
c->rt->EndDraw();
bmp->Width = width;
bmp->Height = height;
bmp->Stride = width*4;
return bmp;
}
void uiDrawBitmapUpdate(uiDrawBitmap* bmp, const void* data)
{
D2D1_RECT_U rekt = D2D1::RectU(0, 0, bmp->Width, bmp->Height);
bmp->bmp->CopyFromMemory(&rekt, data, bmp->Stride);
}
void uiDrawBitmapDraw(uiDrawContext* c, uiDrawBitmap* bmp, uiRect* srcrect, uiRect* dstrect)
{
D2D_RECT_F _srcrect = D2D1::RectF(srcrect->X, srcrect->Y, srcrect->X+srcrect->Width-1, srcrect->Y+srcrect->Height-1);
D2D_RECT_F _dstrect = D2D1::RectF(dstrect->X, dstrect->Y, dstrect->X+dstrect->Width-1, dstrect->Y+dstrect->Height-1);
c->rt->DrawBitmap(bmp->bmp, &_dstrect, 1.0f, D2D1_BITMAP_INTERPOLATION_MODE_LINEAR, &_srcrect);
}
void uiDrawFreeBitmap(uiDrawBitmap* bmp)
{
bmp->bmp->Release();
uiFree(bmp);
}

View File

@ -9,6 +9,14 @@ struct uiDrawContext {
ID2D1PathGeometry *currentClip; ID2D1PathGeometry *currentClip;
}; };
struct uiDrawBitmap {
int Width;
int Height;
int Stride;
ID2D1Bitmap* bmp;
};
// drawpath.cpp // drawpath.cpp
extern ID2D1PathGeometry *pathGeometry(uiDrawPath *p); extern ID2D1PathGeometry *pathGeometry(uiDrawPath *p);

View File

@ -27,11 +27,93 @@
#include "../version.h" #include "../version.h"
SDL_Window* MainWindow; uiWindow* MainWindow;
SDL_GLContext MainGL; uiArea* MainDrawArea;
void RunMainWindow(); SDL_Thread* EmuThread;
int EmuRunning;
u32 derpo[256*384];
uiDrawBitmap* test = NULL;
int EmuThreadFunc(void* burp)
{
// init shit.
for (int i = 0; i < 256*384; i++)
{
if (i >= 256*192)
{
if (i&1) derpo[i] = 0xFF0000FF;
else derpo[i] = 0xFF00FF00;
}
else
{
if (i&1) derpo[i] = 0xFFFF0000;
else derpo[i] = 0xFFFFFF00;
}
}
while (EmuRunning != 0)
{
if (EmuRunning == 1)
{
// emulate
printf("dfdssdf\n");
}
else
{
// paused
uiAreaQueueRedrawAll(MainDrawArea);
SDL_Delay(50);
}
}
return 44203;
}
void OnAreaDraw(uiAreaHandler* handler, uiArea* area, uiAreaDrawParams* params)
{
if (!test) test = uiDrawNewBitmap(params->Context, 256, 384);
uiRect dorp = {0, 0, 256, 384};
uiDrawBitmapUpdate(test, derpo);
uiDrawBitmapDraw(params->Context, test, &dorp, &dorp);
//printf("draw\n");
}
void OnAreaMouseEvent(uiAreaHandler* handler, uiArea* area, uiAreaMouseEvent* evt)
{
//
}
void OnAreaMouseCrossed(uiAreaHandler* handler, uiArea* area, int left)
{
//
}
void OnAreaDragBroken(uiAreaHandler* handler, uiArea* area)
{
//
}
int OnAreaKeyEvent(uiAreaHandler* handler, uiArea* area, uiAreaKeyEvent* evt)
{
printf("key event: %04X %02X\n", evt->ExtKey, evt->Key);
uiAreaQueueRedrawAll(MainDrawArea);
return 1;
}
int OnCloseWindow(uiWindow* window, void* blarg)
{
uiQuit();
return 1;
}
void OnOpenFile(uiMenuItem* item, uiWindow* window, void* blarg) void OnOpenFile(uiMenuItem* item, uiWindow* window, void* blarg)
{ {
@ -41,6 +123,7 @@ void OnOpenFile(uiMenuItem* item, uiWindow* window, void* blarg)
printf("file opened: %s\n", file); printf("file opened: %s\n", file);
} }
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
srand(time(NULL)); srand(time(NULL));
@ -64,8 +147,6 @@ int main(int argc, char** argv)
return 1; return 1;
} }
//RunMainWindow();
uiMenu* menu; uiMenu* menu;
uiMenuItem* menuitem; uiMenuItem* menuitem;
@ -77,10 +158,29 @@ int main(int argc, char** argv)
uiWindow* win; uiWindow* win;
win = uiNewWindow("melonDS " MELONDS_VERSION, 256, 384, 1); win = uiNewWindow("melonDS " MELONDS_VERSION, 256, 384, 1);
uiWindowOnClosing(win, OnCloseWindow, NULL);
uiAreaHandler areahandler;
areahandler.Draw = OnAreaDraw;
areahandler.MouseEvent = OnAreaMouseEvent;
areahandler.MouseCrossed = OnAreaMouseCrossed;
areahandler.DragBroken = OnAreaDragBroken;
areahandler.KeyEvent = OnAreaKeyEvent;
MainDrawArea = uiNewArea(&areahandler);
uiWindowSetChild(win, uiControl(MainDrawArea));
//uiWindowSetChild(win, uiControl(uiNewButton("become a girl")));
EmuRunning = 2;
EmuThread = SDL_CreateThread(EmuThreadFunc, "melonDS magic", NULL);
uiControlShow(uiControl(win)); uiControlShow(uiControl(win));
uiMain(); uiMain();
EmuRunning = 0;
SDL_WaitThread(EmuThread, NULL);
uiUninit(); uiUninit();
SDL_Quit(); SDL_Quit();
return 0; return 0;
@ -122,42 +222,3 @@ int CALLBACK WinMain(HINSTANCE hinst, HINSTANCE hprev, LPSTR cmdline, int cmdsho
} }
#endif #endif
void RunMainWindow()
{
MainWindow = SDL_CreateWindow("melonDS " MELONDS_VERSION,
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
640, 480,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
//MainGL= SDL_GL_CreateContext(MainWindow);
// event loop
bool run = true;
while (run)
{
SDL_Event evt;
while (SDL_PollEvent(&evt))
{
switch (evt.type)
{
case SDL_WINDOWEVENT:
if (evt.window.event == SDL_WINDOWEVENT_CLOSE)
{
run = false;
break;
}
break;
}
}
// do extra shit here
/*glClearColor(1, 0, 1, 1);
glClear(GL_COLOR_BUFFER_BIT);
SDL_GL_SwapWindow(MainWindow);
SDL_Delay(50);*/
}
//SDL_GL_DeleteContext(MainGL);
SDL_DestroyWindow(MainWindow);
}