2012-07-07 08:08:28 +04:00
|
|
|
/*
|
|
|
|
* clock-win
|
|
|
|
*
|
|
|
|
* Windowed Clock Application
|
|
|
|
*
|
|
|
|
* Creates a window that displays an analog clock.
|
|
|
|
*/
|
2012-03-29 05:34:28 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <time.h>
|
2012-12-11 08:28:31 +04:00
|
|
|
#include <syscall.h>
|
2012-03-29 05:34:28 +04:00
|
|
|
|
|
|
|
struct timeval {
|
|
|
|
unsigned int tv_sec;
|
|
|
|
unsigned int tv_usec;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define PI 3.14159265
|
|
|
|
|
|
|
|
#include <ft2build.h>
|
|
|
|
#include FT_FREETYPE_H
|
|
|
|
#include FT_CACHE_H
|
|
|
|
|
|
|
|
#include "lib/window.h"
|
|
|
|
#include "lib/graphics.h"
|
|
|
|
#include "lib/decorations.h"
|
|
|
|
|
|
|
|
sprite_t * sprites[128];
|
|
|
|
sprite_t alpha_tmp;
|
|
|
|
|
|
|
|
uint16_t win_width;
|
|
|
|
uint16_t win_height;
|
|
|
|
|
|
|
|
window_t * window;
|
Context-based graphics library.
All graphics library commands now take a gfx_context_t pointer, which
points to a simple datastructure describing a rendering context (width,
height, depth, total size, front buffer, backbuffer; where backbuffer =
front buffer when not in double-buffering mode, thus we always render to
backbuffer except on a flip). This may have caused a minor speed
reduction, but I don't really care as it's far more important that we
support multiple graphics contexts.
TODO:
- Shared Memory Fonts library (there are a couple of apps that use these
so-called "shmem fonts" on their own; we need a dedicated library for
them)
- Break off "TTK" GUI toolkit into its own library. Since it's just a
callback-based button framework, this shouldn't be too hard right now.
Also, with the previous tick, I'll be able to put labels on controls
and start using text in more places.
2012-04-17 22:21:34 +04:00
|
|
|
gfx_context_t * w_ctx;
|
2012-03-29 05:34:28 +04:00
|
|
|
|
|
|
|
int center_x(int x) {
|
|
|
|
return (win_width - x) / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
int center_y(int y) {
|
|
|
|
return (win_height - y) / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
void init_sprite(int i, char * filename, char * alpha) {
|
|
|
|
sprites[i] = malloc(sizeof(sprite_t));
|
|
|
|
load_sprite(sprites[i], filename);
|
|
|
|
if (alpha) {
|
|
|
|
sprites[i]->alpha = 1;
|
|
|
|
load_sprite(&alpha_tmp, alpha);
|
|
|
|
sprites[i]->masks = alpha_tmp.bitmap;
|
|
|
|
} else {
|
|
|
|
sprites[i]->alpha = 0;
|
|
|
|
}
|
|
|
|
sprites[i]->blank = 0x0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t min(int32_t a, int32_t b) {
|
|
|
|
return (a < b) ? a : b;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t max(int32_t a, int32_t b) {
|
|
|
|
return (a > b) ? a : b;
|
|
|
|
}
|
|
|
|
void draw(int secs) {
|
|
|
|
struct tm * timeinfo = localtime((time_t *)&secs);
|
|
|
|
#if 0
|
|
|
|
printf("Hour: %d\n", timeinfo->tm_hour);
|
|
|
|
printf("Min: %d\n", timeinfo->tm_min);
|
|
|
|
printf("Sec: %d\n", timeinfo->tm_sec);
|
|
|
|
#endif
|
Context-based graphics library.
All graphics library commands now take a gfx_context_t pointer, which
points to a simple datastructure describing a rendering context (width,
height, depth, total size, front buffer, backbuffer; where backbuffer =
front buffer when not in double-buffering mode, thus we always render to
backbuffer except on a flip). This may have caused a minor speed
reduction, but I don't really care as it's far more important that we
support multiple graphics contexts.
TODO:
- Shared Memory Fonts library (there are a couple of apps that use these
so-called "shmem fonts" on their own; we need a dedicated library for
them)
- Break off "TTK" GUI toolkit into its own library. Since it's just a
callback-based button framework, this shouldn't be too hard right now.
Also, with the previous tick, I'll be able to put labels on controls
and start using text in more places.
2012-04-17 22:21:34 +04:00
|
|
|
draw_fill(w_ctx, rgb(255,255,255));
|
2012-03-29 05:34:28 +04:00
|
|
|
|
2012-03-29 21:12:06 +04:00
|
|
|
{
|
|
|
|
int r1 = win_width * 3 / 7;
|
|
|
|
int r2 = win_width / 2;
|
|
|
|
for (int val = 0; val < 12; val += 1) {
|
|
|
|
double _val = (float)val / 12.0;
|
|
|
|
_val *= 2.0 * PI;
|
Context-based graphics library.
All graphics library commands now take a gfx_context_t pointer, which
points to a simple datastructure describing a rendering context (width,
height, depth, total size, front buffer, backbuffer; where backbuffer =
front buffer when not in double-buffering mode, thus we always render to
backbuffer except on a flip). This may have caused a minor speed
reduction, but I don't really care as it's far more important that we
support multiple graphics contexts.
TODO:
- Shared Memory Fonts library (there are a couple of apps that use these
so-called "shmem fonts" on their own; we need a dedicated library for
them)
- Break off "TTK" GUI toolkit into its own library. Since it's just a
callback-based button framework, this shouldn't be too hard right now.
Also, with the previous tick, I'll be able to put labels on controls
and start using text in more places.
2012-04-17 22:21:34 +04:00
|
|
|
draw_line(w_ctx,
|
|
|
|
decor_left_width + win_width / 2 + r1 * sin(_val), decor_left_width + win_width / 2 + r2 * sin(_val),
|
2012-03-29 21:12:06 +04:00
|
|
|
decor_top_height + win_width / 2 - r1 * cos(_val), decor_top_height + win_width / 2 - r2 * cos(_val), rgb(0,0,0));
|
|
|
|
}
|
|
|
|
}
|
2012-03-29 05:34:28 +04:00
|
|
|
{ /* Hours */
|
|
|
|
double val = timeinfo->tm_hour;
|
2012-03-29 21:12:06 +04:00
|
|
|
val += (double)timeinfo->tm_min / 60.0;
|
2012-03-29 05:34:28 +04:00
|
|
|
if (val > 12.0)
|
|
|
|
val -= 12.0;
|
|
|
|
val /= 12.0;
|
|
|
|
val *= 2.0 * PI;
|
|
|
|
int radius = win_width * 1 / 4;
|
|
|
|
int left = win_width / 2 + radius * sin(val);
|
|
|
|
int top = win_width / 2 - radius * cos(val);
|
|
|
|
uint32_t color = rgb(0,0,0);
|
Context-based graphics library.
All graphics library commands now take a gfx_context_t pointer, which
points to a simple datastructure describing a rendering context (width,
height, depth, total size, front buffer, backbuffer; where backbuffer =
front buffer when not in double-buffering mode, thus we always render to
backbuffer except on a flip). This may have caused a minor speed
reduction, but I don't really care as it's far more important that we
support multiple graphics contexts.
TODO:
- Shared Memory Fonts library (there are a couple of apps that use these
so-called "shmem fonts" on their own; we need a dedicated library for
them)
- Break off "TTK" GUI toolkit into its own library. Since it's just a
callback-based button framework, this shouldn't be too hard right now.
Also, with the previous tick, I'll be able to put labels on controls
and start using text in more places.
2012-04-17 22:21:34 +04:00
|
|
|
draw_line_thick(w_ctx, decor_left_width + win_width / 2, decor_left_width + left, decor_top_height + win_width / 2, decor_top_height + top, color, 2);
|
2012-03-29 05:34:28 +04:00
|
|
|
}
|
|
|
|
{ /* Minutes */
|
|
|
|
double val = timeinfo->tm_min;
|
2012-03-29 21:12:06 +04:00
|
|
|
val += (double)timeinfo->tm_sec / 60.0;
|
2012-03-29 05:34:28 +04:00
|
|
|
val /= 60.0;
|
|
|
|
val *= 2.0 * PI;
|
|
|
|
int radius = win_width * 3 / 7;
|
|
|
|
int left = win_width / 2 + radius * sin(val);
|
|
|
|
int top = win_width / 2 - radius * cos(val);
|
|
|
|
uint32_t color = rgb(0,0,0);
|
Context-based graphics library.
All graphics library commands now take a gfx_context_t pointer, which
points to a simple datastructure describing a rendering context (width,
height, depth, total size, front buffer, backbuffer; where backbuffer =
front buffer when not in double-buffering mode, thus we always render to
backbuffer except on a flip). This may have caused a minor speed
reduction, but I don't really care as it's far more important that we
support multiple graphics contexts.
TODO:
- Shared Memory Fonts library (there are a couple of apps that use these
so-called "shmem fonts" on their own; we need a dedicated library for
them)
- Break off "TTK" GUI toolkit into its own library. Since it's just a
callback-based button framework, this shouldn't be too hard right now.
Also, with the previous tick, I'll be able to put labels on controls
and start using text in more places.
2012-04-17 22:21:34 +04:00
|
|
|
draw_line_thick(w_ctx, decor_left_width + win_width / 2, decor_left_width + left, decor_top_height + win_width / 2, decor_top_height + top, color, 1);
|
2012-03-29 05:34:28 +04:00
|
|
|
}
|
|
|
|
{ /* Seconds */
|
|
|
|
double val = timeinfo->tm_sec;
|
|
|
|
val /= 60.0;
|
|
|
|
val *= 2.0 * PI;
|
|
|
|
int radius = win_width * 3 / 7;
|
|
|
|
int left = win_width / 2 + radius * sin(val);
|
|
|
|
int top = win_width / 2 - radius * cos(val);
|
|
|
|
uint32_t color = rgb(255,0,0);
|
Context-based graphics library.
All graphics library commands now take a gfx_context_t pointer, which
points to a simple datastructure describing a rendering context (width,
height, depth, total size, front buffer, backbuffer; where backbuffer =
front buffer when not in double-buffering mode, thus we always render to
backbuffer except on a flip). This may have caused a minor speed
reduction, but I don't really care as it's far more important that we
support multiple graphics contexts.
TODO:
- Shared Memory Fonts library (there are a couple of apps that use these
so-called "shmem fonts" on their own; we need a dedicated library for
them)
- Break off "TTK" GUI toolkit into its own library. Since it's just a
callback-based button framework, this shouldn't be too hard right now.
Also, with the previous tick, I'll be able to put labels on controls
and start using text in more places.
2012-04-17 22:21:34 +04:00
|
|
|
draw_line(w_ctx, decor_left_width + win_width / 2, decor_left_width + left, decor_top_height + win_width / 2, decor_top_height + top, color);
|
2012-03-29 05:34:28 +04:00
|
|
|
}
|
|
|
|
|
2012-11-19 08:41:30 +04:00
|
|
|
render_decorations(window, w_ctx, "Clock");
|
Context-based graphics library.
All graphics library commands now take a gfx_context_t pointer, which
points to a simple datastructure describing a rendering context (width,
height, depth, total size, front buffer, backbuffer; where backbuffer =
front buffer when not in double-buffering mode, thus we always render to
backbuffer except on a flip). This may have caused a minor speed
reduction, but I don't really care as it's far more important that we
support multiple graphics contexts.
TODO:
- Shared Memory Fonts library (there are a couple of apps that use these
so-called "shmem fonts" on their own; we need a dedicated library for
them)
- Break off "TTK" GUI toolkit into its own library. Since it's just a
callback-based button framework, this shouldn't be too hard right now.
Also, with the previous tick, I'll be able to put labels on controls
and start using text in more places.
2012-04-17 22:21:34 +04:00
|
|
|
flip(w_ctx);
|
2012-03-29 05:34:28 +04:00
|
|
|
}
|
|
|
|
|
2012-10-17 07:42:48 +04:00
|
|
|
void resize_callback(window_t * win) {
|
|
|
|
win_width = win->width;
|
|
|
|
win_height = win->height;
|
|
|
|
reinit_graphics_window(w_ctx, window);
|
|
|
|
}
|
|
|
|
|
2012-03-29 05:34:28 +04:00
|
|
|
int main (int argc, char ** argv) {
|
|
|
|
setup_windowing();
|
|
|
|
|
|
|
|
int left = 100;
|
|
|
|
int top = 100;
|
|
|
|
int width = 200;
|
|
|
|
int height = 200;
|
|
|
|
|
|
|
|
win_width = width;
|
|
|
|
win_height = height;
|
|
|
|
|
2012-10-17 07:42:48 +04:00
|
|
|
resize_window_callback = resize_callback;
|
|
|
|
|
2012-03-29 05:34:28 +04:00
|
|
|
/* Do something with a window */
|
|
|
|
window = window_create(left, top, width + decor_width(), height + decor_height());
|
Context-based graphics library.
All graphics library commands now take a gfx_context_t pointer, which
points to a simple datastructure describing a rendering context (width,
height, depth, total size, front buffer, backbuffer; where backbuffer =
front buffer when not in double-buffering mode, thus we always render to
backbuffer except on a flip). This may have caused a minor speed
reduction, but I don't really care as it's far more important that we
support multiple graphics contexts.
TODO:
- Shared Memory Fonts library (there are a couple of apps that use these
so-called "shmem fonts" on their own; we need a dedicated library for
them)
- Break off "TTK" GUI toolkit into its own library. Since it's just a
callback-based button framework, this shouldn't be too hard right now.
Also, with the previous tick, I'll be able to put labels on controls
and start using text in more places.
2012-04-17 22:21:34 +04:00
|
|
|
w_ctx = init_graphics_window_double_buffer(window);
|
2012-03-29 05:34:28 +04:00
|
|
|
init_decorations();
|
|
|
|
|
|
|
|
struct timeval now;
|
|
|
|
int last = 0;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
syscall_gettimeofday(&now, NULL); //time(NULL);
|
|
|
|
if (now.tv_sec != last) {
|
|
|
|
last = now.tv_sec;
|
|
|
|
draw(last);
|
|
|
|
}
|
|
|
|
char ch = 0;
|
|
|
|
w_keyboard_t * kbd;
|
|
|
|
do {
|
2012-12-10 11:07:04 +04:00
|
|
|
kbd = poll_keyboard_async();
|
2012-03-29 05:34:28 +04:00
|
|
|
if (kbd != NULL) {
|
|
|
|
ch = kbd->key;
|
|
|
|
free(kbd);
|
|
|
|
}
|
|
|
|
} while (kbd != NULL);
|
|
|
|
if (ch == 'q') {
|
|
|
|
goto done;
|
|
|
|
break;
|
|
|
|
}
|
2012-12-11 08:28:31 +04:00
|
|
|
syscall_nanosleep(0,50);
|
2012-03-29 05:34:28 +04:00
|
|
|
}
|
|
|
|
done:
|
|
|
|
|
|
|
|
teardown_windowing();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|