Skip to content

Commit

Permalink
linux_video FreeGlut handling base example ; added X11 part in video.h
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamOnAir committed Nov 23, 2024
1 parent d1a24a9 commit d9bdbbd
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 31 deletions.
30 changes: 25 additions & 5 deletions include/video.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,31 @@
#define VIDEO_H

/* X11 */
void createWindow(int height, int width);
void displayWindowText(const char *msg);
void setWindowIcon(const char *iconPath);
#ifdef __linux__

/* OpenGL */
#define KEY_ESCAPE 9
#define KEY_SPACEBAR 65
#define KEY_UP 111
#define KEY_RIGHT 114
#define KEY_DOWN 116
#define KEY_LEFT 113

static Display *d;
static Window w;
static XEvent e;
static int s;
static GC gc;

void axCreateWindow(int width, int height);
void axDrawRect(unsigned long color, int x, int y, int width, int height);
void axClearWindow();
int axGetNextEvent(XEvent *event);
void axCloseWindow();
int axGetNextEvent(XEvent *event)

#endif

/* FreeGLUT (cross-platform) */

typedef struct Window {
void (*create)(int width, int height, const char* title);
Expand All @@ -16,7 +36,7 @@ typedef struct Window {
void (*drawText)(float x, float y, const char* text);
void (*drawButton)(float x, float y, float width, float height, const char* label);
void (*destroy)(void);
int (*isButtonClicked)(float x, float y, float width, float height)
int (*isButtonClicked)(float x, float y, float width, float height);
} Window;

Window* createWindowInstance(void);
Expand Down
18 changes: 9 additions & 9 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@
#include <GL/glut.h>
#include "include/video.h"

static const char* display_text = "Hello OpenGL!";
static const char* display_text = "yOU ARE A KING !";

int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

Window* window = createWindowInstance();
window->create(800, 600, "OpenGL Window");
window->create(800, 600, "Best game ever");

glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClearColor(0.0f, 1.0f, 1.0f, 0.0f);

while (!window->shouldClose()) {
window->pollEvents();
glClear(GL_COLOR_BUFFER_BIT);

// Draw button background in white
glColor3f(1.0f, 1.0f, 1.0f);
window->drawButton(-0.5f, -0.5f, 1.0f, 0.3f, "");
// Draw button background
glColor3f(0.0,0.0,0.0);
window->drawButton(-0.5f, -0.5f, 1.0f, 0.3f, "BUD");

// Draw button text in black
glColor3f(0.0f, 0.0f, 0.0f);
// Text
glColor3f(0.0f, 0.0f, 1.0f);
window->drawText(-0.2f, -0.4f, "Click Me");

// Draw header text in white
glColor3f(1.0f, 1.0f, 1.0f);
glColor3f(0.0f, 0.0f, 1.0f);
window->drawText(-0.8f, 0.8f, display_text);

if (window->isButtonClicked(-0.5f, -0.5f, 1.0f, 0.3f)) {
Expand Down
65 changes: 48 additions & 17 deletions src/system/gnu_linux/linux_video.c
Original file line number Diff line number Diff line change
@@ -1,38 +1,69 @@
// NOTE: Any system running X11 can be handled by linux_video

#ifdef __linux__

#include "../../../include/video.h"
#include <stdio.h>
#include <stdlib.h>

/* X11 */

#ifdef __linux__

#include <X11/Xlib.h>

static Display *d;
static Window w;
static XEvent e;
static int s;
static GC gc;

void createWindow(int width, int height) {

d = XOpenDisplay(NULL);
w = XCreateSimpleWindow(d, RootWindow(d,s), 0, 0, width, height, 1,
BlackPixel(d,s), WhitePixel(d,s));
void axCreateWindow(int width, int height) {
d = XOpenDisplay(NULL);
if (d == NULL) {
printf("Cannot open display\n");
exit(1);
}

s = DefaultScreen(d);
s = DefaultScreen(d);
w = XCreateSimpleWindow(d, RootWindow(d,s), 0, 0, width, height, 1,
BlackPixel(d,s), WhitePixel(d,s));

// Enable key press and expose events
XSelectInput(d, w, KeyPressMask | ExposureMask);

if (d == NULL) {
printf("Cannot open display");
exit(1);
}

XMapWindow(d, w);
XPending(d);
// Create graphics context
gc = DefaultGC(d, s);

XMapWindow(d, w);
XFlush(d);
}

void axDrawRect(unsigned long color, int x, int y, int width, int height) {
XSetForeground(d, gc, color);
XFillRectangle(d, w, gc, x, y, width, height);
XFlush(d);
}

void axClearWindow() {
XClearWindow(d, w);
XFlush(d);
}

int axGetNextEvent(XEvent *event) {
if (XPending(d)) {
XNextEvent(d, event);
return 1;
}
return 0;
}

void axCloseWindow() {
XCloseDisplay(d);
}

#endif

/* FreeGLUT (cross-platform) */



/*
Copyright (C) 2024 Ellouze Adam <[email protected]>
Expand Down

0 comments on commit d9bdbbd

Please sign in to comment.