add clock

git-svn-id: http://svn2.nishi.boats/svn/milsko/trunk@245 b9cfdab3-6d41-4d17-bbe4-086880011989
This commit is contained in:
NishiOwO
2025-10-09 15:20:49 +00:00
parent a8f2628009
commit 4a23bf61b3
10 changed files with 187 additions and 7 deletions

View File

@@ -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)

View File

@@ -278,6 +278,9 @@
<dd>
<a href="#Mw_Widget_OpenGL_h__MwOpenGLSwapBuffer">MwOpenGLSwapBuffer</a>
</dd>
<dd>
<a href="#Mw_Widget_OpenGL_h__MwOpenGLSetColor">MwOpenGLSetColor</a>
</dd>
<dt>
<a href="#Mw_Widget_ScrollBar_h">Mw/Widget/ScrollBar.h</a>
</dt>
@@ -1948,6 +1951,28 @@
</dd>
</dl>
<hr>
<pre id="Mw_Widget_OpenGL_h__MwOpenGLSetColor">MWDECL <B><FONT COLOR="#228B22">void</FONT></B> <B><FONT COLOR="#0000FF">MwOpenGLSetColor</FONT></B> (
MwWidget handle,
MwLLColor color
);</pre>
<dl>
<dd>
Set color using glColor3f.
</dd>
<dt>
Parameter <code>handle</code>
</dt>
<dd>
Widget.
</dd>
<dt>
Parameter <code>color</code>
</dt>
<dd>
Color.
</dd>
</dl>
<hr>
<h2 align="center" id="Mw_Widget_ScrollBar_h">Mw/Widget/ScrollBar.h</h2>
<dl>
<dt>

133
examples/glclock.c Normal file
View File

@@ -0,0 +1,133 @@
/* $Id$ */
#include <Mw/Milsko.h>
#include <Mw/Widget/OpenGL.h>
#include <time.h>
#include <GL/gl.h>
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);
}

View File

@@ -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

View File

@@ -17,6 +17,7 @@
#define MwNareaShown "IareaShown"
#define MwNchecked "Ichecked"
#define MwNalignment "Ialignment"
#define MwNbold "Ibold"
#define MwNtitle "Stitle"
#define MwNtext "Stext"

View File

@@ -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);

View File

@@ -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);
}

View File

@@ -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;
}

View File

@@ -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);

View File

@@ -9,6 +9,7 @@
#endif
#include <GL/gl.h>
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
}