From 4a23bf61b359cd39cb22a30f60fad06efc91bb07 Mon Sep 17 00:00:00 2001 From: NishiOwO Date: Thu, 9 Oct 2025 15:20:49 +0000 Subject: [PATCH] add clock git-svn-id: http://svn2.nishi.boats/svn/milsko/trunk@245 b9cfdab3-6d41-4d17-bbe4-086880011989 --- GNUmakefile | 4 +- doc/index.html | 25 +++++++ examples/glclock.c | 133 +++++++++++++++++++++++++++++++++++ include/Mw/Draw.h | 9 +++ include/Mw/StringDefs.h | 1 + include/MwOO/Widget/OpenGL.h | 1 + oosrc/widget/opengl.cc | 3 + src/draw.c | 6 ++ src/widget/label.c | 3 +- src/widget/opengl.c | 9 +-- 10 files changed, 187 insertions(+), 7 deletions(-) create mode 100644 examples/glclock.c diff --git a/GNUmakefile b/GNUmakefile index e2969cc..78141aa 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -7,7 +7,7 @@ endif CC = $(GCC)gcc CXX = $(GCC)g++ -CFLAGS = -Wall -Wextra -Wno-unused-parameter -Wno-implicit-fallthrough -Iinclude +CFLAGS = -Wall -Wextra -Wno-unused-parameter -Wno-implicit-fallthrough -Wno-unused-value -Iinclude LDFLAGS = LIBS = @@ -138,7 +138,7 @@ EXAMPLES = examples/example$(EXEC) examples/rotate$(EXEC) examples/image$(EXEC) ifeq ($(OPENGL),1) L_OBJS += src/widget/opengl.o OOL_OBJS += oosrc/widget/opengl.o -EXAMPLES += examples/gltriangle$(EXEC) examples/glgears$(EXEC) examples/glboing$(EXEC) examples/glcube$(EXEC) +EXAMPLES += examples/glclock$(EXEC) examples/gltriangle$(EXEC) examples/glgears$(EXEC) examples/glboing$(EXEC) examples/glcube$(EXEC) endif ifeq ($(VULKAN),1) diff --git a/doc/index.html b/doc/index.html index ad2f47c..21404f8 100644 --- a/doc/index.html +++ b/doc/index.html @@ -278,6 +278,9 @@
MwOpenGLSwapBuffer
+
+ MwOpenGLSetColor +
Mw/Widget/ScrollBar.h
@@ -1948,6 +1951,28 @@
+
MWDECL void MwOpenGLSetColor (
+	MwWidget handle,
+	MwLLColor color
+);
+
+
+ Set color using glColor3f. +
+
+ Parameter handle +
+
+ Widget. +
+
+ Parameter color +
+
+ Color. +
+
+

Mw/Widget/ScrollBar.h

diff --git a/examples/glclock.c b/examples/glclock.c new file mode 100644 index 0000000..4b56f32 --- /dev/null +++ b/examples/glclock.c @@ -0,0 +1,133 @@ +/* $Id$ */ +#include +#include + +#include + +#include + +MwWidget window, opengl, ldate, ltime; +time_t last = 0; +struct tm* tm; +int br, bg, bb; +int fr, fg, fb; + +double deg2rad(double deg) { + return (360.0 - deg) / 180.0 * M_PI; +} + +void tick(MwWidget handle, void* user, void* call) { + time_t t = time(NULL); + int i; + double rad; + if(last != t) { + char* wday[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; + char* mon[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; + + char buf[512]; + tm = localtime(&t); + + sprintf(buf, "%s %02d %s", mon[tm->tm_mon], tm->tm_mday, wday[tm->tm_wday]); + MwSetText(ldate, MwNtext, buf); + + sprintf(buf, "%02d:%02d %s", tm->tm_hour % 12, tm->tm_min, tm->tm_hour >= 12 ? "PM" : "AM"); + MwSetText(ltime, MwNtext, buf); + + last = t; + } + + MwOpenGLMakeCurrent(opengl); + + glClearColor(br / 255.0, bg / 255.0, bb / 255.0, 1); + glClear(GL_COLOR_BUFFER_BIT); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(-1, 1, -1, 1, 0, 100); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + glLineWidth(2); + for(i = 0; i < 12; i++) { + rad = deg2rad(30.0 * i); + glPointSize((i % 3) ? 2 : 5); + glBegin(GL_POINTS); + glColor3f(fr / 255.0, fg / 255.0, fb / 255.0); + glVertex2f(cos(rad) * 0.85, sin(rad) * 0.85); + glEnd(); + } + + rad = deg2rad((tm->tm_hour % 12) * 30 + (tm->tm_min / 2) - 90); + glBegin(GL_LINES); + glVertex2f(0, 0); + glVertex2f(cos(rad) * 0.3, sin(rad) * 0.3); + glEnd(); + + rad = deg2rad(tm->tm_min * 6 - 90); + glBegin(GL_LINES); + glVertex2f(0, 0); + glVertex2f(cos(rad) * 0.5, sin(rad) * 0.5); + glEnd(); + + MwOpenGLSwapBuffer(opengl); +} + +void resize(MwWidget handle, void* user, void* call) { + int w = MwGetInteger(handle, MwNwidth); + int h = MwGetInteger(handle, MwNheight); + + MwVaApply(opengl, + MwNwidth, w / 5 * 2, + MwNheight, h, + NULL); + + MwVaApply(ldate, + MwNx, w / 5 * 2, + MwNwidth, w - w / 5 * 2, + MwNheight, h / 2, + NULL); + + MwVaApply(ltime, + MwNx, w / 5 * 2, + MwNy, h / 2, + MwNwidth, w - w / 5 * 2, + MwNheight, h / 2, + NULL); + + glViewport(0, 0, w / 5 * 2, h); +} + +int main() { + MwLLColor bgcolor, fgcolor; + + window = MwVaCreateWidget(MwWindowClass, "main", NULL, 0, 0, 250, 100, + MwNtitle, "clock", + NULL); + + bgcolor = MwParseColor(window, MwGetText(window, MwNbackground)); + MwGetColor(bgcolor, &br, &bg, &bb); + + fgcolor = MwParseColor(window, MwGetText(window, MwNforeground)); + MwGetColor(fgcolor, &fr, &fg, &fb); + + opengl = MwCreateWidget(MwOpenGLClass, "clock", window, 0, 0, 100, 100); + + ldate = MwVaCreateWidget(MwLabelClass, "date", window, 100, 0, 150, 50, + MwNbold, 1, + NULL); + ltime = MwVaCreateWidget(MwLabelClass, "time", window, 100, 50, 150, 50, + MwNbold, 1, + NULL); + + MwAddUserHandler(window, MwNtickHandler, tick, NULL); + MwAddUserHandler(window, MwNresizeHandler, resize, NULL); + + resize(window, NULL, NULL); + tick(window, NULL, NULL); + + MwLoop(window); + + MwLLFreeColor(fgcolor); + MwLLFreeColor(bgcolor); +} diff --git a/include/Mw/Draw.h b/include/Mw/Draw.h index a37701e..24906e2 100644 --- a/include/Mw/Draw.h +++ b/include/Mw/Draw.h @@ -106,6 +106,15 @@ MWDECL int MwTextWidth(MwWidget handle, const char* text); */ MWDECL int MwTextHeight(MwWidget handle, const char* text); +/*! + * %brief Get color components + * %param color Color + * %param red Pointer to red color + * %param green Pointer to green color + * %param blue Pointer to blue color + */ +MWDECL void MwGetColor(MwLLColor color, int* red, int* green, int* blue); + #ifdef __cplusplus } #endif diff --git a/include/Mw/StringDefs.h b/include/Mw/StringDefs.h index 2264917..59a4127 100644 --- a/include/Mw/StringDefs.h +++ b/include/Mw/StringDefs.h @@ -17,6 +17,7 @@ #define MwNareaShown "IareaShown" #define MwNchecked "Ichecked" #define MwNalignment "Ialignment" +#define MwNbold "Ibold" #define MwNtitle "Stitle" #define MwNtext "Stext" diff --git a/include/MwOO/Widget/OpenGL.h b/include/MwOO/Widget/OpenGL.h index 72b3009..cb54d42 100644 --- a/include/MwOO/Widget/OpenGL.h +++ b/include/MwOO/Widget/OpenGL.h @@ -11,6 +11,7 @@ class OpenGL : public MwOO::Base { void MakeCurrent(void); void* GetProcAddress(const char* name); void SwapBuffer(void); + void SetColor(MwLLColor color); void SetBackground(const char* value); const char* GetBackground(void); void SetForeground(const char* value); diff --git a/oosrc/widget/opengl.cc b/oosrc/widget/opengl.cc index 78c6cc2..5c6634a 100644 --- a/oosrc/widget/opengl.cc +++ b/oosrc/widget/opengl.cc @@ -14,6 +14,9 @@ void* MwOO::OpenGL::GetProcAddress(const char* name){ void MwOO::OpenGL::SwapBuffer(void){ MwOpenGLSwapBuffer(this->widget); } +void MwOO::OpenGL::SetColor(MwLLColor color){ + MwOpenGLSetColor(this->widget, color); +} void MwOO::OpenGL::SetBackground(const char* value){ MwSetText(this->widget, MwNbackground, value); } diff --git a/src/draw.c b/src/draw.c index c10315f..dbe52bc 100644 --- a/src/draw.c +++ b/src/draw.c @@ -556,3 +556,9 @@ MwLLPixmap MwLoadImage(MwWidget handle, const char* path) { return px; } + +void MwGetColor(MwLLColor color, int* red, int* green, int* blue) { + *red = color->red; + *green = color->green; + *blue = color->blue; +} diff --git a/src/widget/label.c b/src/widget/label.c index cfd4f1b..76e58d3 100644 --- a/src/widget/label.c +++ b/src/widget/label.c @@ -4,6 +4,7 @@ static int create(MwWidget handle) { MwSetDefault(handle); MwSetInteger(handle, MwNalignment, MwALIGNMENT_CENTER); + MwSetInteger(handle, MwNbold, 0); return 0; } @@ -34,7 +35,7 @@ static void draw(MwWidget handle) { p.x = r.width - MwTextWidth(handle, str) / 2; } p.y = r.height / 2; - MwDrawText(handle, &p, str, 0, MwALIGNMENT_CENTER, text); + MwDrawText(handle, &p, str, MwGetInteger(handle, MwNbold), MwALIGNMENT_CENTER, text); MwLLFreeColor(text); MwLLFreeColor(base); diff --git a/src/widget/opengl.c b/src/widget/opengl.c index 668b172..fccb46e 100644 --- a/src/widget/opengl.c +++ b/src/widget/opengl.c @@ -9,6 +9,7 @@ #endif #include +typedef void(GLAPIENTRY* MWglColor3f)(GLfloat red, GLfloat green, GLfloat blue); #ifdef _WIN32 typedef HGLRC(WINAPI* MWwglCreateContext)(HDC); typedef BOOL(WINAPI* MWwglMakeCurrent)(HDC, HGLRC); @@ -25,6 +26,7 @@ typedef struct opengl { MWwglMakeCurrent wglMakeCurrent; MWwglDeleteContext wglDeleteContext; MWwglGetProcAddress wglGetProcAddress; + MWglColor3f glColor3f; } opengl_t; #else typedef XVisualInfo* (*MWglXChooseVisual)(Display* dpy, int screen, int* attribList); @@ -46,6 +48,7 @@ typedef struct opengl { MWglXMakeCurrent glXMakeCurrent; MWglXSwapBuffers glXSwapBuffers; MWglXGetProcAddress glXGetProcAddress; + MWglColor3f glColor3f; } opengl_t; #endif @@ -73,6 +76,7 @@ static int create(MwWidget handle) { o->wglMakeCurrent = (MWwglMakeCurrent)(void*)GetProcAddress(o->lib, "wglMakeCurrent"); o->wglDeleteContext = (MWwglDeleteContext)(void*)GetProcAddress(o->lib, "wglDeleteContext"); o->wglGetProcAddress = (MWwglGetProcAddress)(void*)GetProcAddress(o->lib, "wglGetProcAddress"); + o->glColor3f = (MWglColor3f)(void*)GetProcAddress(o->lib, "glColor3f"); o->gl = o->wglCreateContext(o->dc); #else @@ -98,6 +102,7 @@ static int create(MwWidget handle) { o->glXMakeCurrent = (MWglXMakeCurrent)dlsym(o->lib, "glXMakeCurrent"); o->glXSwapBuffers = (MWglXSwapBuffers)dlsym(o->lib, "glXSwapBuffers"); o->glXGetProcAddress = (MWglXGetProcAddress)dlsym(o->lib, "glXGetProcAddress"); + o->glColor3f = (MWglColor3f)dlsym(o->lib, "glColor3f"); /* XXX: fix this */ o->visual = o->glXChooseVisual(handle->lowlevel->display, DefaultScreen(handle->lowlevel->display), attribs); @@ -169,12 +174,8 @@ void MwOpenGLSwapBuffer(MwWidget handle) { void* MwOpenGLGetProcAddress(MwWidget handle, const char* name) { opengl_t* o = (opengl_t*)handle->internal; #ifdef _WIN32 - (void)handle; - return o->wglGetProcAddress(name); #else - (void)handle; - return o->glXGetProcAddress((const GLubyte*)name); #endif }