]> git.lizzy.rs Git - dragonblocks_alpha.git/commitdiff
Add GLFW window
authorElias Fleckenstein <eliasfleckenstein@web.de>
Wed, 24 Mar 2021 14:27:07 +0000 (15:27 +0100)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Wed, 24 Mar 2021 14:27:07 +0000 (15:27 +0100)
src/Makefile
src/client.c
src/signal.c
src/signal.h

index 98ea2a2382b94838b5b867d4a7a2211d3d22db52..0b3e6c5745f089ccf09b494c9bf79e83470a1ca2 100644 (file)
@@ -2,6 +2,7 @@ COMMON = array.o binsearch.o linkedlist.o map.o signal.o util.o types.o
 SERVER = $(COMMON) server.o servercommands.o
 CLIENT = $(COMMON) client.o clientcommands.o
 LIBRARIES = -lpthread -lm
+CLIENT_LIBRARIES = -lGL -lGLEW -lglfw
 FLAGS = -g
 
 ifdef RELEASE
@@ -11,7 +12,7 @@ endif
 all: Dragonblocks DragonblocksServer
 
 Dragonblocks: $(CLIENT)
-       cc $(FLAGS) -o Dragonblocks $(CLIENT) $(LIBRARIES)
+       cc $(FLAGS) -o Dragonblocks $(CLIENT) $(LIBRARIES) $(CLIENT_LIBRARIES)
 
 DragonblocksServer: $(SERVER)
        cc $(FLAGS) -o DragonblocksServer $(SERVER) $(LIBRARIES)
index db0068d4be262770ad25f627a893a6949779e642..9ce1e6a7a6a5e16528ede8032fef4eeb6e8762af 100644 (file)
@@ -5,6 +5,9 @@
 #include <unistd.h>
 #include <errno.h>
 #include <netdb.h>
+#include <GL/glew.h>
+#include <GL/gl.h>
+#include <GLFW/glfw3.h>
 #include "client.h"
 #include "signal.h"
 #include "util.h"
@@ -47,97 +50,78 @@ static void *reciever_thread(void *cliptr)
 
 static void client_loop(Client *client)
 {
-       while (client->state != CS_DISCONNECTED) {
-               if (client->state == CS_CREATED) {
-                       printf("Enter name: ");
-                       fflush(stdout);
-                       char name[NAME_MAX];
-                       if (scanf("%s", name) == EOF)
-                               return;
-                       client->name = strdup(name);
-                       pthread_mutex_lock(&client->mtx);
-                       if (write_u32(client->fd, SC_AUTH) && write(client->fd, client->name, strlen(name) + 1)) {
-                               client->state = CS_AUTH;
-                               printf("Authenticating...\n");
-                       }
-                       pthread_mutex_unlock(&client->mtx);
-               } else if (client->state == CS_ACTIVE) {
-                       printf("%s: ", client->name);
-                       fflush(stdout);
-                       char buffer[BUFSIZ] = {0};
-                       if (scanf("%s", buffer) == EOF)
-                               return;
-                       if (strcmp(buffer, "disconnect") == 0) {
-                               return;
-                       } else if (strcmp(buffer, "setnode") == 0) {
-                               v3s32 pos;
-                               char node[BUFSIZ] = {0};
-                               if (scanf("%d %d %d %s", &pos.x, &pos.y, &pos.z, node) == EOF)
-                                       return;
-                               Node node_type = NODE_INVALID;
-                               if (strcmp(node, "air") == 0)
-                                       node_type = NODE_AIR;
-                               else if (strcmp(node, "grass") == 0)
-                                       node_type = NODE_GRASS;
-                               else if (strcmp(node, "dirt") == 0)
-                                       node_type = NODE_DIRT;
-                               else if (strcmp(node, "stone") == 0)
-                                       node_type = NODE_STONE;
-                               if (node_type == NODE_INVALID) {
-                                       printf("Invalid node\n");
-                               } else {
-                                       pthread_mutex_lock(&client->mtx);
-                                       (void) (write_u32(client->fd, SC_SETNODE) && write_v3s32(client->fd, pos) && write_u32(client->fd, node_type));
-                                       pthread_mutex_unlock(&client->mtx);
-                               }
-                       } else if (strcmp(buffer, "getnode") == 0) {
-                               v3s32 pos;
-                               if (scanf("%d %d %d", &pos.x, &pos.y, &pos.z) == EOF)
-                                       return;
-                               pthread_mutex_lock(&client->mtx);
-                               (void) (write_u32(client->fd, SC_GETBLOCK) && write_v3s32(client->fd, map_node_to_block_pos(pos, NULL)));
-                               pthread_mutex_unlock(&client->mtx);
-                       } else if (strcmp(buffer, "printnode") == 0) {
-                               v3s32 pos;
-                               if (scanf("%d %d %d", &pos.x, &pos.y, &pos.z) == EOF)
-                                       return;
-                               MapNode node = map_get_node(client->map, pos);
-                               const char *nodename;
-                               switch (node.type) {
-                                       case NODE_UNLOADED:
-                                               nodename = "unloaded";
-                                               break;
-                                       case NODE_AIR:
-                                               nodename = "air";
-                                               break;
-                                       case NODE_GRASS:
-                                               nodename = "grass";
-                                               break;
-                                       case NODE_DIRT:
-                                               nodename = "dirt";
-                                               break;
-                                       case NODE_STONE:
-                                               nodename = "stone";
-                                               break;
-                                       case NODE_INVALID:
-                                               nodename = "invalid";
-                                               break;
-                               }
-                               printf("%s\n", nodename);
-                       } else if (strcmp(buffer, "kick") == 0) {
-                               char target_name[NAME_MAX];
-                               if (scanf("%s", target_name) == EOF)
-                                       return;
-                               pthread_mutex_lock(&client->mtx);
-                               (void) (write_u32(client->fd, SC_KICK) && write(client->fd, target_name, strlen(target_name) + 1) != -1);
-                               pthread_mutex_unlock(&client->mtx);
-                       } else {
-                               printf("Invalid command: %s\n", buffer);
-                       }
-               } else {
-                       sched_yield();
+       if(! glfwInit()) {
+               printf("Failed to initialize GLFW\n");
+               return;
+       }
+
+       glfwWindowHint(GLFW_SAMPLES, 8);
+       glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
+       glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
+       glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
+
+       GLFWwindow *window = glfwCreateWindow(1024, 768, "Dragonblocks", NULL, NULL);
+
+       if (! window) {
+               printf("Failed to create window\n");
+               glfwTerminate();
+               return;
+       }
+
+       glfwMakeContextCurrent(window);
+       if (glewInit() != GLEW_OK) {
+               printf("Failed to initialize GLEW\n");
+               return;
+       }
+
+       while (! glfwWindowShouldClose(window) && client->state != CS_DISCONNECTED && ! interrupted) {
+               glClear(GL_COLOR_BUFFER_BIT);
+               glClearColor(0.52941176470588, 0.8078431372549, 0.92156862745098, 1.0);
+
+               glfwSwapBuffers(window);
+               glfwPollEvents();
+       }
+}
+
+static bool client_name_prompt(Client *client)
+{
+       printf("Enter name: ");
+       fflush(stdout);
+       char name[NAME_MAX];
+       if (scanf("%s", name) == EOF)
+               return false;
+       client->name = strdup(name);
+       pthread_mutex_lock(&client->mtx);
+       if (write_u32(client->fd, SC_AUTH) && write(client->fd, client->name, strlen(name) + 1)) {
+               client->state = CS_AUTH;
+               printf("Authenticating...\n");
+       }
+       pthread_mutex_unlock(&client->mtx);
+       return true;
+}
+
+static bool client_authenticate(Client *client)
+{
+       for ever {
+               switch (client->state) {
+                       case CS_CREATED:
+                               if (client_name_prompt(client))
+                                       break;
+                               else
+                                       return false;
+                       case CS_AUTH:
+                               if (interrupted)
+                                       return false;
+                               else
+                                       sched_yield();
+                               break;
+                       case CS_ACTIVE:
+                               return true;
+                       case CS_DISCONNECTED:
+                               return false;
                }
        }
+       return false;
 }
 
 int main(int argc, char **argv)
@@ -191,7 +175,8 @@ int main(int argc, char **argv)
        pthread_t recv_thread;
        pthread_create(&recv_thread, NULL, &reciever_thread, &client);
 
-       client_loop(&client);
+       if (client_authenticate(&client))
+               client_loop(&client);
 
        client_disconnect(&client, true, NULL);
 
index e5ac22c9c16e52b60e2821520f1b8ca8ce6edf9b..5c8c43551714e42f57d2003431b6e36ebb30f8a5 100644 (file)
@@ -1,9 +1,13 @@
 #include <signal.h>
 #include <stdio.h>
 #include <string.h>
+#include "signal.h"
+
+bool interrupted = false;
 
 static void interrupt_handler(int sig)
 {
+       interrupted = true;
        fprintf(stderr, "%s\n", strsignal(sig));
 }
 
index e927ae1037a562ec2518c5aa30a829f35c3c4602..ec3234bc101da764c614ceeba3eaa9cdb52d601c 100644 (file)
@@ -1,6 +1,9 @@
 #ifndef _SIGNAL_H_
 #define _SIGNAL_H_
 
+#include <stdbool.h>
+
+extern bool interrupted;
 void init_signal_handlers();
 
 #endif