fixed some win32 issues

This commit is contained in:
vurtun 2015-04-29 15:56:46 +02:00
parent a86e33fe90
commit 51bb40dcc2
2 changed files with 40 additions and 11 deletions

View File

@ -1,3 +1,8 @@
/*
Copyright (c) 2015
vurtun <polygone@gmx.net>
MIT licence
*/
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdarg.h> #include <stdarg.h>
@ -25,6 +30,8 @@
typedef struct XFont { typedef struct XFont {
HFONT handle; HFONT handle;
int height; int height;
int ascent;
int descent;
} XFont; } XFont;
typedef struct XSurface { typedef struct XSurface {
@ -44,6 +51,8 @@ typedef struct XWindow {
RECT rect; RECT rect;
XSurface *backbuffer; XSurface *backbuffer;
XFont *font; XFont *font;
unsigned int width;
unsigned int height;
} XWindow; } XWindow;
struct demo { struct demo {
@ -65,6 +74,8 @@ struct demo {
}; };
static int running = 1; static int running = 1;
static int quit = 0;
static XWindow xw;
static void static void
die(const char *fmt, ...) die(const char *fmt, ...)
@ -86,13 +97,21 @@ timestamp(LARGE_INTEGER freq)
static XFont* static XFont*
font_new(const char *name, int height) font_new(HDC hdc, const char *name, int height)
{ {
HFONT old;
TEXTMETRIC metrics;
XFont *font = malloc(sizeof(XFont)); XFont *font = malloc(sizeof(XFont));
font->height = height; font->height = height;
font->handle = CreateFont(height, 0, 0, 0, 0, FALSE, FALSE, FALSE, font->handle = CreateFont(height, 0, 0, 0, 0, FALSE, FALSE, FALSE,
ANSI_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS, ANSI_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
ANTIALIASED_QUALITY, DEFAULT_PITCH, name); ANTIALIASED_QUALITY, DEFAULT_PITCH, name);
old = SelectObject(hdc, font->handle);
GetTextMetrics(hdc, &metrics);
font->ascent = metrics.tmAscent;
font->descent = metrics.tmDescent;
SelectObject(hdc, old);
return font; return font;
} }
@ -164,8 +183,6 @@ surface_draw_line(XSurface *surf, short x0, short y0, short x1, short y1,
{ {
HPEN hPen; HPEN hPen;
HPEN old; HPEN old;
/* TODO(vurtun): This brush allocation is literally bad shit INSANE!
* There has to be a better way to do this! */
hPen = CreatePen(PS_SOLID, 1, RGB(r,g,b)); hPen = CreatePen(PS_SOLID, 1, RGB(r,g,b));
old = SelectObject(surf->hdc, hPen); old = SelectObject(surf->hdc, hPen);
MoveToEx(surf->hdc, x0, y0, NULL); MoveToEx(surf->hdc, x0, y0, NULL);
@ -236,6 +253,7 @@ surface_draw_text(XSurface *surf, XFont *font, short x, short y, unsigned short
UINT bg = RGB(bg_r, bg_g, bg_b); UINT bg = RGB(bg_r, bg_g, bg_b);
UINT fg = RGB(fg_r, fg_g, fg_b); UINT fg = RGB(fg_r, fg_g, fg_b);
HFONT old = SelectObject(surf->hdc, font->handle); HFONT old = SelectObject(surf->hdc, font->handle);
format.left = x; format.left = x;
format.top = y; format.top = y;
format.right = x + w; format.right = x + w;
@ -301,7 +319,8 @@ draw(XSurface *surf, struct gui_command_list *list)
} break; } break;
case GUI_COMMAND_TEXT: { case GUI_COMMAND_TEXT: {
struct gui_command_text *t = (void*)cmd; struct gui_command_text *t = (void*)cmd;
surface_draw_text(surf, t->font, t->x, t->y, t->w, t->h, (const char*)t->string, XWindow *xw = t->font;
surface_draw_text(surf, xw->font, t->x, t->y, t->w, t->h, (const char*)t->string,
t->length, t->bg.r, t->bg.g, t->bg.b, t->fg.r, t->fg.g, t->fg.b); t->length, t->bg.r, t->bg.g, t->bg.b, t->fg.r, t->fg.g, t->fg.b);
} break; } break;
default: break; default: break;
@ -406,6 +425,14 @@ wnd_proc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
case WM_DESTROY: case WM_DESTROY:
PostQuitMessage(WM_QUIT); PostQuitMessage(WM_QUIT);
running = 0; running = 0;
quit = 1;
break;
case WM_SIZE:
if (xw.backbuffer) {
xw.width = LOWORD(lParam);
xw.height = HIWORD(lParam);
surface_resize(xw.backbuffer, xw.hdc, xw.width, xw.height);
}
break; break;
default: default:
return DefWindowProc(hWnd, msg, wParam, lParam); return DefWindowProc(hWnd, msg, wParam, lParam);
@ -434,8 +461,7 @@ WinMain(HINSTANCE hInstance, HINSTANCE prev, LPSTR lpCmdLine, int show)
struct gui_panel panel; struct gui_panel panel;
/* Window */ /* Window */
XWindow xw; QueryPerformanceFrequency(&freq);
memset(&xw, 0, sizeof(xw));
xw.wc.style = CS_HREDRAW|CS_VREDRAW; xw.wc.style = CS_HREDRAW|CS_VREDRAW;
xw.wc.lpfnWndProc = wnd_proc; xw.wc.lpfnWndProc = wnd_proc;
xw.wc.hInstance = hInstance; xw.wc.hInstance = hInstance;
@ -448,11 +474,12 @@ WinMain(HINSTANCE hInstance, HINSTANCE prev, LPSTR lpCmdLine, int show)
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
0, 0, hInstance, 0); 0, 0, hInstance, 0);
QueryPerformanceFrequency(&freq);
xw.hdc = GetDC(xw.hWnd); xw.hdc = GetDC(xw.hWnd);
GetClientRect(xw.hWnd, &xw.rect); GetClientRect(xw.hWnd, &xw.rect);
xw.backbuffer = surface_new(xw.hdc, xw.rect.right, xw.rect.bottom); xw.backbuffer = surface_new(xw.hdc, xw.rect.right, xw.rect.bottom);
xw.font = font_new("Times New Roman", 12); xw.font = font_new(xw.hdc, "Times New Roman", 14);
xw.width = xw.rect.right;
xw.height = xw.rect.bottom;
/* GUI */ /* GUI */
memset(&in, 0, sizeof in); memset(&in, 0, sizeof in);
@ -476,7 +503,7 @@ WinMain(HINSTANCE hInstance, HINSTANCE prev, LPSTR lpCmdLine, int show)
demo.slider = 2.0f; demo.slider = 2.0f;
demo.prog = 60; demo.prog = 60;
while (running) { while (running && !quit) {
/* Input */ /* Input */
MSG msg; MSG msg;
start = timestamp(freq); start = timestamp(freq);
@ -494,7 +521,7 @@ WinMain(HINSTANCE hInstance, HINSTANCE prev, LPSTR lpCmdLine, int show)
gui_input_end(&in); gui_input_end(&in);
/* GUI */ /* GUI */
gui_buffer_begin(&canvas, &buffer, xw.backbuffer->width, xw.backbuffer->height); gui_buffer_begin(&canvas, &buffer, xw.width, xw.height);
running = gui_panel_begin(&layout, &panel, "Demo", &canvas, &in); running = gui_panel_begin(&layout, &panel, "Demo", &canvas, &in);
demo_panel(&layout, &demo); demo_panel(&layout, &demo);
gui_panel_end(&layout, &panel); gui_panel_end(&layout, &panel);

4
gui.c
View File

@ -2042,6 +2042,7 @@ gui_panel_tab_begin(struct gui_panel_layout *parent, struct gui_panel_layout *ta
flags = GUI_PANEL_BORDER|GUI_PANEL_MINIMIZABLE|GUI_PANEL_TAB; flags = GUI_PANEL_BORDER|GUI_PANEL_MINIMIZABLE|GUI_PANEL_TAB;
gui_panel_init(&panel,bounds.x,bounds.y,bounds.w,null_rect.h,flags,parent->config,&parent->font); gui_panel_init(&panel,bounds.x,bounds.y,bounds.w,null_rect.h,flags,parent->config,&parent->font);
panel.minimized = minimized; panel.minimized = minimized;
gui_panel_begin(tab, &panel, title, canvas, parent->input); gui_panel_begin(tab, &panel, title, canvas, parent->input);
unify(&clip, &parent->clip, tab->clip.x, tab->clip.y, tab->clip.x + tab->clip.w, unify(&clip, &parent->clip, tab->clip.x, tab->clip.y, tab->clip.x + tab->clip.w,
tab->clip.y + tab->clip.h); tab->clip.y + tab->clip.h);
@ -2063,6 +2064,7 @@ gui_panel_tab_end(struct gui_panel_layout *parent, struct gui_panel_layout *tab)
panel.y = tab->y; panel.y = tab->y;
panel.flags = GUI_PANEL_BORDER|GUI_PANEL_MINIMIZABLE|GUI_PANEL_TAB; panel.flags = GUI_PANEL_BORDER|GUI_PANEL_MINIMIZABLE|GUI_PANEL_TAB;
gui_panel_end(tab, &panel); gui_panel_end(tab, &panel);
parent->at_y += tab->height + tab->config->item_spacing.y; parent->at_y += tab->height + tab->config->item_spacing.y;
canvas = parent->canvas; canvas = parent->canvas;
canvas->scissor(canvas->userdata, parent->clip.x,parent->clip.y,parent->clip.w,parent->clip.h); canvas->scissor(canvas->userdata, parent->clip.x,parent->clip.y,parent->clip.w,parent->clip.h);
@ -2092,9 +2094,9 @@ gui_panel_group_begin(struct gui_panel_layout *parent, struct gui_panel_layout *
gui_panel_alloc_space(&bounds, parent); gui_panel_alloc_space(&bounds, parent);
zero(group, sizeof(*group)); zero(group, sizeof(*group));
canvas = parent->canvas; canvas = parent->canvas;
flags = GUI_PANEL_BORDER|GUI_PANEL_SCROLLBAR|GUI_PANEL_TAB; flags = GUI_PANEL_BORDER|GUI_PANEL_SCROLLBAR|GUI_PANEL_TAB;
gui_panel_init(&panel, bounds.x,bounds.y,bounds.w,bounds.h,flags,parent->config,&parent->font); gui_panel_init(&panel, bounds.x,bounds.y,bounds.w,bounds.h,flags,parent->config,&parent->font);
gui_panel_begin(group, &panel, title, parent->canvas, parent->input); gui_panel_begin(group, &panel, title, parent->canvas, parent->input);
group->offset = offset; group->offset = offset;
unify(&clip, &parent->clip, group->clip.x, group->clip.y, group->clip.x + group->clip.w, unify(&clip, &parent->clip, group->clip.x, group->clip.y, group->clip.x + group->clip.w,