2008-12-02 23:15:01 +03:00
|
|
|
/*
|
2008-12-08 03:59:11 +03:00
|
|
|
* Copyright © 2008 Kristian Høgsberg
|
|
|
|
*
|
2015-06-11 08:48:59 +03:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
2008-12-08 03:59:11 +03:00
|
|
|
*
|
2015-06-11 08:48:59 +03:00
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
* Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
2008-12-02 23:15:01 +03:00
|
|
|
*/
|
|
|
|
|
2011-01-07 22:42:49 +03:00
|
|
|
#include "config.h"
|
|
|
|
|
2008-12-08 03:59:11 +03:00
|
|
|
#include <stdint.h>
|
2014-08-10 18:47:45 +04:00
|
|
|
#include <stdbool.h>
|
2008-12-08 03:59:11 +03:00
|
|
|
#include <stdio.h>
|
2008-11-03 23:31:30 +03:00
|
|
|
#include <stdlib.h>
|
2008-12-08 03:59:11 +03:00
|
|
|
#include <string.h>
|
2008-11-03 23:31:30 +03:00
|
|
|
#include <math.h>
|
2008-12-08 03:59:11 +03:00
|
|
|
#include <time.h>
|
2019-04-27 00:57:31 +03:00
|
|
|
#include <errno.h>
|
2008-12-08 03:59:11 +03:00
|
|
|
|
2008-11-03 23:31:30 +03:00
|
|
|
#include <GL/gl.h>
|
2010-03-03 17:54:29 +03:00
|
|
|
#include <EGL/egl.h>
|
|
|
|
#include <EGL/eglext.h>
|
2008-12-08 03:59:11 +03:00
|
|
|
|
2012-04-24 21:28:26 +04:00
|
|
|
#include <linux/input.h>
|
2011-11-22 16:18:50 +04:00
|
|
|
#include <wayland-client.h>
|
2008-12-08 03:59:11 +03:00
|
|
|
|
|
|
|
#include "window.h"
|
|
|
|
|
2008-11-03 23:31:30 +03:00
|
|
|
struct gears {
|
2008-12-08 03:59:11 +03:00
|
|
|
struct window *window;
|
2012-01-10 21:23:19 +04:00
|
|
|
struct widget *widget;
|
2008-12-08 03:59:11 +03:00
|
|
|
|
2009-01-27 07:42:46 +03:00
|
|
|
struct display *d;
|
2008-12-08 03:59:11 +03:00
|
|
|
|
|
|
|
EGLDisplay display;
|
2011-02-19 01:00:55 +03:00
|
|
|
EGLDisplay config;
|
2008-12-08 03:59:11 +03:00
|
|
|
EGLContext context;
|
|
|
|
GLfloat angle;
|
|
|
|
|
2012-04-24 21:28:26 +04:00
|
|
|
struct {
|
|
|
|
GLfloat rotx;
|
|
|
|
GLfloat roty;
|
|
|
|
} view;
|
|
|
|
|
|
|
|
int button_down;
|
|
|
|
int last_x, last_y;
|
|
|
|
|
2008-12-08 03:59:11 +03:00
|
|
|
GLint gear_list[3];
|
2012-07-25 16:33:42 +04:00
|
|
|
int fullscreen;
|
2012-07-25 17:19:23 +04:00
|
|
|
int frames;
|
|
|
|
uint32_t last_fps;
|
2008-11-03 23:31:30 +03:00
|
|
|
};
|
|
|
|
|
2008-12-08 03:59:11 +03:00
|
|
|
struct gear_template {
|
|
|
|
GLfloat material[4];
|
|
|
|
GLfloat inner_radius;
|
|
|
|
GLfloat outer_radius;
|
|
|
|
GLfloat width;
|
|
|
|
GLint teeth;
|
|
|
|
GLfloat tooth_depth;
|
|
|
|
};
|
|
|
|
|
2012-03-30 19:52:39 +04:00
|
|
|
static const struct gear_template gear_templates[] = {
|
2008-12-08 03:59:11 +03:00
|
|
|
{ { 0.8, 0.1, 0.0, 1.0 }, 1.0, 4.0, 1.0, 20, 0.7 },
|
|
|
|
{ { 0.0, 0.8, 0.2, 1.0 }, 0.5, 2.0, 2.0, 10, 0.7 },
|
2015-05-15 18:17:47 +03:00
|
|
|
{ { 0.2, 0.2, 1.0, 1.0 }, 1.3, 2.0, 0.5, 10, 0.7 },
|
2008-12-08 03:59:11 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static GLfloat light_pos[4] = {5.0, 5.0, 10.0, 0.0};
|
|
|
|
|
|
|
|
static void die(const char *msg)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s", msg);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2008-11-03 23:31:30 +03:00
|
|
|
static void
|
2008-12-08 03:59:11 +03:00
|
|
|
make_gear(const struct gear_template *t)
|
2008-11-03 23:31:30 +03:00
|
|
|
{
|
|
|
|
GLint i;
|
|
|
|
GLfloat r0, r1, r2;
|
|
|
|
GLfloat angle, da;
|
|
|
|
GLfloat u, v, len;
|
|
|
|
|
2008-12-08 03:59:11 +03:00
|
|
|
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, t->material);
|
2008-11-03 23:31:30 +03:00
|
|
|
|
2008-12-08 03:59:11 +03:00
|
|
|
r0 = t->inner_radius;
|
|
|
|
r1 = t->outer_radius - t->tooth_depth / 2.0;
|
|
|
|
r2 = t->outer_radius + t->tooth_depth / 2.0;
|
|
|
|
|
|
|
|
da = 2.0 * M_PI / t->teeth / 4.0;
|
2008-11-03 23:31:30 +03:00
|
|
|
|
|
|
|
glShadeModel(GL_FLAT);
|
|
|
|
|
|
|
|
glNormal3f(0.0, 0.0, 1.0);
|
|
|
|
|
|
|
|
/* draw front face */
|
|
|
|
glBegin(GL_QUAD_STRIP);
|
2008-12-08 03:59:11 +03:00
|
|
|
for (i = 0; i <= t->teeth; i++) {
|
|
|
|
angle = i * 2.0 * M_PI / t->teeth;
|
|
|
|
glVertex3f(r0 * cos(angle), r0 * sin(angle), t->width * 0.5);
|
|
|
|
glVertex3f(r1 * cos(angle), r1 * sin(angle), t->width * 0.5);
|
|
|
|
if (i < t->teeth) {
|
|
|
|
glVertex3f(r0 * cos(angle), r0 * sin(angle), t->width * 0.5);
|
|
|
|
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), t->width * 0.5);
|
2008-11-03 23:31:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
glEnd();
|
|
|
|
|
|
|
|
/* draw front sides of teeth */
|
|
|
|
glBegin(GL_QUADS);
|
2008-12-08 03:59:11 +03:00
|
|
|
da = 2.0 * M_PI / t->teeth / 4.0;
|
|
|
|
for (i = 0; i < t->teeth; i++) {
|
|
|
|
angle = i * 2.0 * M_PI / t->teeth;
|
|
|
|
|
|
|
|
glVertex3f(r1 * cos(angle), r1 * sin(angle), t->width * 0.5);
|
|
|
|
glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), t->width * 0.5);
|
|
|
|
glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), t->width * 0.5);
|
|
|
|
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), t->width * 0.5);
|
2008-11-03 23:31:30 +03:00
|
|
|
}
|
|
|
|
glEnd();
|
|
|
|
|
|
|
|
glNormal3f(0.0, 0.0, -1.0);
|
|
|
|
|
|
|
|
/* draw back face */
|
|
|
|
glBegin(GL_QUAD_STRIP);
|
2008-12-08 03:59:11 +03:00
|
|
|
for (i = 0; i <= t->teeth; i++) {
|
|
|
|
angle = i * 2.0 * M_PI / t->teeth;
|
|
|
|
glVertex3f(r1 * cos(angle), r1 * sin(angle), -t->width * 0.5);
|
|
|
|
glVertex3f(r0 * cos(angle), r0 * sin(angle), -t->width * 0.5);
|
|
|
|
if (i < t->teeth) {
|
|
|
|
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -t->width * 0.5);
|
|
|
|
glVertex3f(r0 * cos(angle), r0 * sin(angle), -t->width * 0.5);
|
2008-11-03 23:31:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
glEnd();
|
|
|
|
|
|
|
|
/* draw back sides of teeth */
|
|
|
|
glBegin(GL_QUADS);
|
2008-12-08 03:59:11 +03:00
|
|
|
da = 2.0 * M_PI / t->teeth / 4.0;
|
|
|
|
for (i = 0; i < t->teeth; i++) {
|
|
|
|
angle = i * 2.0 * M_PI / t->teeth;
|
|
|
|
|
|
|
|
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -t->width * 0.5);
|
|
|
|
glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -t->width * 0.5);
|
|
|
|
glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -t->width * 0.5);
|
|
|
|
glVertex3f(r1 * cos(angle), r1 * sin(angle), -t->width * 0.5);
|
2008-11-03 23:31:30 +03:00
|
|
|
}
|
|
|
|
glEnd();
|
|
|
|
|
|
|
|
/* draw outward faces of teeth */
|
|
|
|
glBegin(GL_QUAD_STRIP);
|
2008-12-08 03:59:11 +03:00
|
|
|
for (i = 0; i < t->teeth; i++) {
|
|
|
|
angle = i * 2.0 * M_PI / t->teeth;
|
2008-11-03 23:31:30 +03:00
|
|
|
|
2008-12-08 03:59:11 +03:00
|
|
|
glVertex3f(r1 * cos(angle), r1 * sin(angle), t->width * 0.5);
|
|
|
|
glVertex3f(r1 * cos(angle), r1 * sin(angle), -t->width * 0.5);
|
2008-11-03 23:31:30 +03:00
|
|
|
u = r2 * cos(angle + da) - r1 * cos(angle);
|
|
|
|
v = r2 * sin(angle + da) - r1 * sin(angle);
|
|
|
|
len = sqrt(u * u + v * v);
|
|
|
|
u /= len;
|
|
|
|
v /= len;
|
|
|
|
glNormal3f(v, -u, 0.0);
|
2008-12-08 03:59:11 +03:00
|
|
|
glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), t->width * 0.5);
|
|
|
|
glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -t->width * 0.5);
|
2008-11-03 23:31:30 +03:00
|
|
|
glNormal3f(cos(angle), sin(angle), 0.0);
|
2008-12-08 03:59:11 +03:00
|
|
|
glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), t->width * 0.5);
|
|
|
|
glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -t->width * 0.5);
|
2008-11-03 23:31:30 +03:00
|
|
|
u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
|
|
|
|
v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
|
|
|
|
glNormal3f(v, -u, 0.0);
|
2008-12-08 03:59:11 +03:00
|
|
|
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), t->width * 0.5);
|
|
|
|
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -t->width * 0.5);
|
2008-11-03 23:31:30 +03:00
|
|
|
glNormal3f(cos(angle), sin(angle), 0.0);
|
|
|
|
}
|
|
|
|
|
2008-12-08 03:59:11 +03:00
|
|
|
glVertex3f(r1 * cos(0), r1 * sin(0), t->width * 0.5);
|
|
|
|
glVertex3f(r1 * cos(0), r1 * sin(0), -t->width * 0.5);
|
2008-11-03 23:31:30 +03:00
|
|
|
|
|
|
|
glEnd();
|
|
|
|
|
|
|
|
glShadeModel(GL_SMOOTH);
|
|
|
|
|
|
|
|
/* draw inside radius cylinder */
|
|
|
|
glBegin(GL_QUAD_STRIP);
|
2008-12-08 03:59:11 +03:00
|
|
|
for (i = 0; i <= t->teeth; i++) {
|
|
|
|
angle = i * 2.0 * M_PI / t->teeth;
|
2008-11-03 23:31:30 +03:00
|
|
|
glNormal3f(-cos(angle), -sin(angle), 0.0);
|
2008-12-08 03:59:11 +03:00
|
|
|
glVertex3f(r0 * cos(angle), r0 * sin(angle), -t->width * 0.5);
|
|
|
|
glVertex3f(r0 * cos(angle), r0 * sin(angle), t->width * 0.5);
|
2008-11-03 23:31:30 +03:00
|
|
|
}
|
|
|
|
glEnd();
|
|
|
|
}
|
|
|
|
|
2012-07-25 17:19:23 +04:00
|
|
|
static void
|
|
|
|
update_fps(struct gears *gears, uint32_t time)
|
|
|
|
{
|
|
|
|
long diff_ms;
|
2014-08-10 18:47:45 +04:00
|
|
|
static bool first_call = true;
|
2012-07-25 17:19:23 +04:00
|
|
|
|
2014-08-10 18:47:45 +04:00
|
|
|
if (first_call) {
|
|
|
|
gears->last_fps = time;
|
|
|
|
first_call = false;
|
|
|
|
} else
|
|
|
|
gears->frames++;
|
2012-07-25 17:19:23 +04:00
|
|
|
|
|
|
|
diff_ms = time - gears->last_fps;
|
|
|
|
|
|
|
|
if (diff_ms > 5000) {
|
|
|
|
float seconds = diff_ms / 1000.0;
|
|
|
|
float fps = gears->frames / seconds;
|
|
|
|
|
|
|
|
printf("%d frames in %6.3f seconds = %6.3f FPS\n", gears->frames, seconds, fps);
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
gears->frames = 0;
|
|
|
|
gears->last_fps = time;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-08 03:59:11 +03:00
|
|
|
static void
|
2012-01-10 19:49:41 +04:00
|
|
|
frame_callback(void *data, struct wl_callback *callback, uint32_t time)
|
|
|
|
{
|
|
|
|
struct gears *gears = data;
|
|
|
|
|
2012-07-25 17:19:23 +04:00
|
|
|
update_fps(gears, time);
|
|
|
|
|
2012-01-10 19:49:41 +04:00
|
|
|
gears->angle = (GLfloat) (time % 8192) * 360 / 8192.0;
|
|
|
|
|
|
|
|
window_schedule_redraw(gears->window);
|
|
|
|
|
|
|
|
if (callback)
|
|
|
|
wl_callback_destroy(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct wl_callback_listener listener = {
|
|
|
|
frame_callback
|
|
|
|
};
|
|
|
|
|
2012-04-24 21:28:26 +04:00
|
|
|
static int
|
|
|
|
motion_handler(struct widget *widget, struct input *input,
|
2012-05-10 20:21:37 +04:00
|
|
|
uint32_t time, float x, float y, void *data)
|
2012-04-24 21:28:26 +04:00
|
|
|
{
|
|
|
|
struct gears *gears = data;
|
|
|
|
int offset_x, offset_y;
|
|
|
|
float step = 0.5;
|
|
|
|
|
|
|
|
if (gears->button_down) {
|
|
|
|
offset_x = x - gears->last_x;
|
|
|
|
offset_y = y - gears->last_y;
|
|
|
|
gears->last_x = x;
|
|
|
|
gears->last_y = y;
|
|
|
|
gears->view.roty += offset_x * step;
|
|
|
|
gears->view.rotx += offset_y * step;
|
|
|
|
if (gears->view.roty >= 360)
|
|
|
|
gears->view.roty = gears->view.roty - 360;
|
|
|
|
if (gears->view.roty <= 0)
|
|
|
|
gears->view.roty = gears->view.roty + 360;
|
|
|
|
if (gears->view.rotx >= 360)
|
|
|
|
gears->view.rotx = gears->view.rotx - 360;
|
|
|
|
if (gears->view.rotx <= 0)
|
|
|
|
gears->view.rotx = gears->view.rotx + 360;
|
|
|
|
}
|
|
|
|
|
2012-05-25 17:01:41 +04:00
|
|
|
return CURSOR_LEFT_PTR;
|
2012-04-24 21:28:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
button_handler(struct widget *widget, struct input *input,
|
2012-05-30 19:31:51 +04:00
|
|
|
uint32_t time, uint32_t button,
|
|
|
|
enum wl_pointer_button_state state, void *data)
|
2012-04-24 21:28:26 +04:00
|
|
|
{
|
|
|
|
struct gears *gears = data;
|
|
|
|
|
|
|
|
if (button == BTN_LEFT) {
|
2012-05-30 19:31:51 +04:00
|
|
|
if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
|
2012-04-24 21:28:26 +04:00
|
|
|
gears->button_down = 1;
|
|
|
|
input_get_position(input,
|
|
|
|
&gears->last_x, &gears->last_y);
|
|
|
|
} else {
|
|
|
|
gears->button_down = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-10 19:49:41 +04:00
|
|
|
static void
|
2012-01-10 21:23:19 +04:00
|
|
|
redraw_handler(struct widget *widget, void *data)
|
2008-11-03 23:31:30 +03:00
|
|
|
{
|
2011-02-19 01:00:55 +03:00
|
|
|
struct rectangle window_allocation;
|
2011-01-11 18:00:52 +03:00
|
|
|
struct rectangle allocation;
|
2012-01-10 19:49:41 +04:00
|
|
|
struct wl_callback *callback;
|
|
|
|
struct gears *gears = data;
|
2010-11-08 23:45:55 +03:00
|
|
|
|
2012-01-11 04:11:42 +04:00
|
|
|
widget_get_allocation(gears->widget, &allocation);
|
2011-02-19 01:00:55 +03:00
|
|
|
window_get_allocation(gears->window, &window_allocation);
|
|
|
|
|
2011-10-07 11:33:06 +04:00
|
|
|
if (display_acquire_window_surface(gears->d,
|
|
|
|
gears->window,
|
|
|
|
gears->context) < 0) {
|
|
|
|
die("Unable to acquire window surface, "
|
|
|
|
"compiled without cairo-egl?\n");
|
|
|
|
}
|
2015-05-15 18:17:47 +03:00
|
|
|
|
2011-02-19 01:00:55 +03:00
|
|
|
glViewport(allocation.x,
|
2012-01-11 04:11:42 +04:00
|
|
|
window_allocation.height - allocation.height - allocation.y,
|
2011-01-11 18:00:52 +03:00
|
|
|
allocation.width, allocation.height);
|
2011-02-19 01:00:55 +03:00
|
|
|
glScissor(allocation.x,
|
|
|
|
window_allocation.height - allocation.height - allocation.y,
|
|
|
|
allocation.width, allocation.height);
|
2010-11-08 23:45:55 +03:00
|
|
|
|
|
|
|
glEnable(GL_SCISSOR_TEST);
|
2008-11-08 23:06:01 +03:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
2008-11-05 15:38:46 +03:00
|
|
|
|
2008-11-03 23:31:30 +03:00
|
|
|
glPushMatrix();
|
|
|
|
|
|
|
|
glTranslatef(0.0, 0.0, -50);
|
|
|
|
|
2012-04-24 21:28:26 +04:00
|
|
|
glRotatef(gears->view.rotx, 1.0, 0.0, 0.0);
|
|
|
|
glRotatef(gears->view.roty, 0.0, 1.0, 0.0);
|
2008-11-03 23:31:30 +03:00
|
|
|
|
|
|
|
glPushMatrix();
|
|
|
|
glTranslatef(-3.0, -2.0, 0.0);
|
2008-12-08 03:59:11 +03:00
|
|
|
glRotatef(gears->angle, 0.0, 0.0, 1.0);
|
|
|
|
glCallList(gears->gear_list[0]);
|
2008-11-03 23:31:30 +03:00
|
|
|
glPopMatrix();
|
|
|
|
|
|
|
|
glPushMatrix();
|
|
|
|
glTranslatef(3.1, -2.0, 0.0);
|
2008-12-08 03:59:11 +03:00
|
|
|
glRotatef(-2.0 * gears->angle - 9.0, 0.0, 0.0, 1.0);
|
|
|
|
glCallList(gears->gear_list[1]);
|
2008-11-03 23:31:30 +03:00
|
|
|
glPopMatrix();
|
|
|
|
|
|
|
|
glPushMatrix();
|
|
|
|
glTranslatef(-3.1, 4.2, 0.0);
|
2008-12-08 03:59:11 +03:00
|
|
|
glRotatef(-2.0 * gears->angle - 25.0, 0.0, 0.0, 1.0);
|
|
|
|
glCallList(gears->gear_list[2]);
|
2008-11-03 23:31:30 +03:00
|
|
|
glPopMatrix();
|
|
|
|
|
|
|
|
glPopMatrix();
|
|
|
|
|
|
|
|
glFlush();
|
2008-12-08 03:59:11 +03:00
|
|
|
|
2011-09-27 23:57:31 +04:00
|
|
|
display_release_window_surface(gears->d, gears->window);
|
2012-01-10 19:49:41 +04:00
|
|
|
|
|
|
|
callback = wl_surface_frame(window_get_wl_surface(gears->window));
|
|
|
|
wl_callback_add_listener(callback, &listener, gears);
|
2008-12-08 03:59:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-01-10 21:23:19 +04:00
|
|
|
resize_handler(struct widget *widget,
|
2011-01-11 18:00:52 +03:00
|
|
|
int32_t width, int32_t height, void *data)
|
2008-12-08 03:59:11 +03:00
|
|
|
{
|
|
|
|
struct gears *gears = data;
|
2012-07-27 07:53:05 +04:00
|
|
|
int32_t size, big, small;
|
2008-12-08 03:59:11 +03:00
|
|
|
|
2011-01-11 18:00:52 +03:00
|
|
|
/* Constrain child size to be square and at least 300x300 */
|
2012-07-27 07:53:05 +04:00
|
|
|
if (width < height) {
|
|
|
|
small = width;
|
|
|
|
big = height;
|
|
|
|
} else {
|
|
|
|
small = height;
|
|
|
|
big = width;
|
2011-01-11 18:00:52 +03:00
|
|
|
}
|
|
|
|
|
2012-07-27 07:53:05 +04:00
|
|
|
if (gears->fullscreen)
|
|
|
|
size = small;
|
|
|
|
else
|
|
|
|
size = big;
|
|
|
|
|
|
|
|
widget_set_size(gears->widget, size, size);
|
2008-12-08 03:59:11 +03:00
|
|
|
}
|
|
|
|
|
2009-09-21 21:47:53 +04:00
|
|
|
static void
|
|
|
|
keyboard_focus_handler(struct window *window,
|
2010-07-29 07:50:12 +04:00
|
|
|
struct input *device, void *data)
|
2009-09-21 21:47:53 +04:00
|
|
|
{
|
2012-01-10 21:23:19 +04:00
|
|
|
window_schedule_redraw(window);
|
2009-09-21 21:47:53 +04:00
|
|
|
}
|
|
|
|
|
2012-07-25 16:33:42 +04:00
|
|
|
static void
|
|
|
|
fullscreen_handler(struct window *window, void *data)
|
|
|
|
{
|
|
|
|
struct gears *gears = data;
|
|
|
|
|
|
|
|
gears->fullscreen ^= 1;
|
|
|
|
window_set_fullscreen(window, gears->fullscreen);
|
|
|
|
}
|
|
|
|
|
2008-12-08 03:59:11 +03:00
|
|
|
static struct gears *
|
2010-06-08 22:59:44 +04:00
|
|
|
gears_create(struct display *display)
|
2008-12-08 03:59:11 +03:00
|
|
|
{
|
2010-12-17 17:53:12 +03:00
|
|
|
const int width = 450, height = 500;
|
2008-12-08 03:59:11 +03:00
|
|
|
struct gears *gears;
|
|
|
|
int i;
|
|
|
|
|
2013-08-08 05:57:05 +04:00
|
|
|
gears = zalloc(sizeof *gears);
|
2009-01-27 07:42:46 +03:00
|
|
|
gears->d = display;
|
2012-02-01 00:24:48 +04:00
|
|
|
gears->window = window_create(display);
|
2013-10-18 05:56:41 +04:00
|
|
|
gears->widget = window_frame_create(gears->window, gears);
|
2011-01-22 02:03:15 +03:00
|
|
|
window_set_title(gears->window, "Wayland Gears");
|
2008-12-08 03:59:11 +03:00
|
|
|
|
2010-06-08 22:59:44 +04:00
|
|
|
gears->display = display_get_egl_display(gears->d);
|
2008-12-08 03:59:11 +03:00
|
|
|
if (gears->display == NULL)
|
|
|
|
die("failed to create egl display\n");
|
|
|
|
|
2010-06-05 18:17:55 +04:00
|
|
|
eglBindAPI(EGL_OPENGL_API);
|
|
|
|
|
2012-01-19 23:05:21 +04:00
|
|
|
gears->config = display_get_argb_egl_config(gears->d);
|
2011-02-19 01:00:55 +03:00
|
|
|
|
|
|
|
gears->context = eglCreateContext(gears->display, gears->config,
|
|
|
|
EGL_NO_CONTEXT, NULL);
|
2008-12-08 03:59:11 +03:00
|
|
|
if (gears->context == NULL)
|
|
|
|
die("failed to create context\n");
|
|
|
|
|
2010-01-25 02:10:15 +03:00
|
|
|
if (!eglMakeCurrent(gears->display, NULL, NULL, gears->context))
|
2012-02-09 21:06:56 +04:00
|
|
|
die("failed to make context current\n");
|
2010-01-25 02:10:15 +03:00
|
|
|
|
2008-12-08 03:59:11 +03:00
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
gears->gear_list[i] = glGenLists(1);
|
|
|
|
glNewList(gears->gear_list[i], GL_COMPILE);
|
|
|
|
make_gear(&gear_templates[i]);
|
|
|
|
glEndList();
|
|
|
|
}
|
|
|
|
|
2012-04-24 21:28:26 +04:00
|
|
|
gears->button_down = 0;
|
|
|
|
gears->last_x = 0;
|
|
|
|
gears->last_y = 0;
|
|
|
|
|
|
|
|
gears->view.rotx = 20.0;
|
|
|
|
gears->view.roty = 30.0;
|
|
|
|
|
2012-07-26 20:55:53 +04:00
|
|
|
printf("Warning: FPS count is limited by the wayland compositor or monitor refresh rate\n");
|
2012-07-25 17:19:23 +04:00
|
|
|
|
2008-12-08 03:59:11 +03:00
|
|
|
glEnable(GL_NORMALIZE);
|
|
|
|
|
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glLoadIdentity();
|
|
|
|
glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 200.0);
|
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
|
|
|
|
glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
|
|
|
|
glEnable(GL_CULL_FACE);
|
|
|
|
glEnable(GL_LIGHTING);
|
|
|
|
glEnable(GL_LIGHT0);
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
glClearColor(0, 0, 0, 0.92);
|
|
|
|
|
2010-06-25 19:19:22 +04:00
|
|
|
window_set_user_data(gears->window, gears);
|
2012-01-10 21:23:19 +04:00
|
|
|
widget_set_resize_handler(gears->widget, resize_handler);
|
|
|
|
widget_set_redraw_handler(gears->widget, redraw_handler);
|
2012-04-24 21:28:26 +04:00
|
|
|
widget_set_button_handler(gears->widget, button_handler);
|
|
|
|
widget_set_motion_handler(gears->widget, motion_handler);
|
2012-01-10 21:23:19 +04:00
|
|
|
window_set_keyboard_focus_handler(gears->window,
|
|
|
|
keyboard_focus_handler);
|
2012-07-25 16:33:42 +04:00
|
|
|
window_set_fullscreen_handler(gears->window, fullscreen_handler);
|
2010-09-03 22:46:38 +04:00
|
|
|
|
2012-01-11 04:11:42 +04:00
|
|
|
window_schedule_resize(gears->window, width, height);
|
2008-12-08 03:59:11 +03:00
|
|
|
|
|
|
|
return gears;
|
|
|
|
}
|
|
|
|
|
2014-05-15 17:28:16 +04:00
|
|
|
static void
|
|
|
|
gears_destroy(struct gears *gears)
|
|
|
|
{
|
|
|
|
widget_destroy(gears->widget);
|
|
|
|
window_destroy(gears->window);
|
|
|
|
free(gears);
|
|
|
|
}
|
|
|
|
|
2008-12-08 03:59:11 +03:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2009-01-27 07:42:46 +03:00
|
|
|
struct display *d;
|
2014-05-15 17:28:16 +04:00
|
|
|
struct gears *gears;
|
2008-12-08 03:59:11 +03:00
|
|
|
|
2013-02-21 00:27:49 +04:00
|
|
|
d = display_create(&argc, argv);
|
2010-11-22 22:34:19 +03:00
|
|
|
if (d == NULL) {
|
2019-04-27 00:57:31 +03:00
|
|
|
fprintf(stderr, "failed to create display: %s\n",
|
|
|
|
strerror(errno));
|
2010-11-22 22:34:19 +03:00
|
|
|
return -1;
|
|
|
|
}
|
2014-05-15 17:28:16 +04:00
|
|
|
gears = gears_create(d);
|
2010-06-08 22:59:44 +04:00
|
|
|
display_run(d);
|
2008-12-08 03:59:11 +03:00
|
|
|
|
2014-05-15 17:28:16 +04:00
|
|
|
gears_destroy(gears);
|
|
|
|
display_destroy(d);
|
|
|
|
|
2008-12-08 03:59:11 +03:00
|
|
|
return 0;
|
|
|
|
}
|