Files
skevity/source/client_source/client.c
2026-04-15 06:57:15 -07:00

167 lines
4.1 KiB
C

#include "client.h"
#include <bits/time.h>
#include <stdlib.h>
#include <string.h>
#include <tart.h>
#include <unistd.h>
#include <signal.h>
#include "../game_source/entities/Player_e.h"
#include "../game_source/entities/Player_c.h"
#include "../game_source/entities/Centery.h"
#include <sys/random.h>
#include <stdio.h>
#include <locale.h>
#include <time.h>
#define jump tart_move_cursor
#define FPS 60
#define FRAME_TIME (1000.0 / FPS)
int e = 1;
void aOnExit() {
//printf("shutting Down");
e = 0;
}
void draw_button(tart_window *w,const t_char* label, tart_vec2 position) {
jump(w, (tart_vec2){position.x,position.y + 0});
tart_printf(w, 1, T"┏━━━");
int size = t_strlen(label);
for(int i = 0; i < size; i++ ) {
tart_printf(w, 1, T"");
}
tart_printf(w, 1, T"━━━┓");
jump(w, (tart_vec2){position.x,position.y + 1});
tart_printf(w, 1, T"┃ %ls ┃", label);
jump(w, (tart_vec2){position.x,position.y + 2});
tart_printf(w, 1, T"┗━━━");
for(int i = 0; i < size; i++ ) {
tart_printf(w, 1, T"");
}
tart_printf(w, 1, T"━━━┛");
}
int limit_fps(int fps, struct timespec* last_time) {
struct timespec current_time;
double frame_time = 1.0 / fps;
clock_gettime(CLOCK_MONOTONIC, &current_time);
double elapsed = (current_time.tv_sec - last_time->tv_sec) +
(current_time.tv_nsec - last_time->tv_nsec) / 1e9;
if (elapsed < frame_time) {
return 0;
}
clock_gettime(CLOCK_MONOTONIC, last_time);
return 1;
}
int client_auth(void* userdata);
int client_init(void* cli) {
setlocale(LC_CTYPE, "en_US.UTF-8");
client* c = (client*)cli;
int x = 0;
c->last_frame_time = clock();
tart_create_window(&c->w, (tart_vec2){150,50}, "testing", c->channel);
tart_init(&c->w);
//jump(&c->w, (tart_vec2){1,1});
//jump(&c->w, (tart_vec2){1,2});
tart_disable_cusor(&c->w);
c->world.size_x = 600;
c->world.size_y = 600;
CreateWorld(&c->world, clock());
InitWorld(&c->world);
player_data p = {
.holding = 0,
.class = 0,
.health = 20,
.ac = 4,
.str = 0,
.wis = 0,
.intel = 0,
.dex = 0,
.name = "testing",
.c = (color){100,100,100},
};
entity* pc = CreatePlayerCharecter(p,c, &c->world);
entity* pe = CreatePlayerEntity(p,0, &c->world);
//pc->position.x = 3;
//pc->position.y = 3;
pe->position.x = 3;
pe->position.y = 3;
//AddEntity(&c->world, pc);
AddEntity(&c->world, pe);
GenerateWorld(&c->world);
CammeraInit(&c->cam, 150,50,0,0,&c->world, &c->w);
clock_gettime(CLOCK_MONOTONIC,&c->last_time);
memset(&c->server, 0, sizeof(game_server));
if(Connect(&c->server) < 0) {
printf("Could not connect to server\n");
return -1;
}
pe->server = c->server.game_info.socket;
return 0;
}
int client_update(ssh_terminal_data* td, void* cli) {
//signal(SIGINT, aOnExit);
client* c = (client*)cli;
tart_window w = c->w;
//tart_init(&w);
if(limit_fps(FPS, &c->last_time) == 1) {
int newSize = tart_draw_to_buffer(&w);
CammeraRender(&c->cam);
tart_jump(&w, (tart_vec2){w.windowSize.x, w.windowSize.y-1});
tart_insert_cell(&w, 5, 'a');
tart_jump(&w, (tart_vec2){0,0});
tart_printf(&w, 5, T"Entities %d\n", c->world.ent_list->count);
}
UpdateWorld(&c->world);
return 0;
}
int client_stop(void* cli) {
client* c = (client*)cli;
printf("Clossing connection\n");
tart_enable_cusor(&c->w);
tart_close(&c->w);
return 0;
}
int client_resize(int x, int y, void *userdata) {
client* c = (client*)userdata;
printf("client address %d\n", userdata);
printf("cammeraSize %d,%d\n", x, y);
int posx = 000000000000;
int posy = 000000000000;
CammeraInit(&c->cam, x, y, posx, posy, &c->world, &c->w);
tart_window_resize(&c->w, (tart_vec2){x-1,y});
//CammeraInit(&c->cam, c->cam.size_x, c->cam.size_y, c->cam.pos_x, c->cam.pos_y, &c->world, &c->w);
//c->cam.tw = &c->w;
return 0;
}