Simple images for grid of ship

main
Cameron Murphy Reikes 2 years ago
parent b99a4076b8
commit a90b5331de

Binary file not shown.

After

Width:  |  Height:  |  Size: 1019 B

@ -11,9 +11,12 @@
#include "sokol_time.h"
#include <enet/enet.h>
#include <process.h> // starting server thread
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include "types.h"
#include <string.h> // errno error message on file open
#include <inttypes.h>
static struct GameState gs = {0};
@ -36,6 +39,7 @@ static ENetHost *client;
static ENetPeer *peer;
static float zoom_target = 300.0f;
static float zoom = 300.0f;
static sg_image image_box;
static void init(void)
{
@ -59,6 +63,34 @@ static void init(void)
exit(-1);
}
// image loading
{
image_box = sg_alloc_image();
int x = 0;
int y = 0;
int comp = 0;
const int desired_channels = 4;
stbi_uc *image_data = stbi_load("loaded/box.png", &x, &y, &comp, desired_channels);
if (!image_data)
{
fprintf(stderr, "Failed to load image: %s\n", stbi_failure_reason());
exit(-1);
}
sg_init_image(image_box, &(sg_image_desc){
.width = x,
.height = y,
.pixel_format = SG_PIXELFORMAT_RGBA8,
.min_filter = SG_FILTER_NEAREST,
.mag_filter = SG_FILTER_NEAREST,
.data.subimage[0][0] = {
.ptr = image_data,
.size = (size_t)(x * y * desired_channels),
}});
stbi_image_free(image_data);
}
// socket initialization
{
if (enet_initialize() != 0)
@ -119,11 +151,16 @@ static void drawbox(V2 gridpos, float rot, V2 bpos, float damage, bool offset_fr
{
sgp_rotate_at(rot, bpos.x, bpos.y);
}
sgp_draw_line(bpos.x - halfbox, bpos.y - halfbox, bpos.x - halfbox, bpos.y + halfbox); // left
sgp_set_image(0, image_box);
sgp_draw_textured_rect(bpos.x - halfbox, bpos.y -halfbox, BOX_SIZE, BOX_SIZE);
sgp_reset_image(0);
/*sgp_draw_line(bpos.x - halfbox, bpos.y - halfbox, bpos.x - halfbox, bpos.y + halfbox); // left
sgp_draw_line(bpos.x - halfbox, bpos.y - halfbox, bpos.x + halfbox, bpos.y - halfbox); // top
sgp_draw_line(bpos.x + halfbox, bpos.y - halfbox, bpos.x + halfbox, bpos.y + halfbox); // right
sgp_draw_line(bpos.x - halfbox, bpos.y + halfbox, bpos.x + halfbox, bpos.y + halfbox); // bottom
sgp_draw_line(bpos.x - halfbox, bpos.y - halfbox, bpos.x + halfbox, bpos.y + halfbox); // diagonal
*/
if (damage > 0.0f)
{
@ -230,7 +267,7 @@ static void frame(void)
bool hand_at_arms_length = false;
{
// interpolate zoom
zoom = lerp(zoom, zoom_target, dt*12.0f);
zoom = lerp(zoom, zoom_target, dt * 12.0f);
// calculate world position and camera
{
@ -401,33 +438,7 @@ static void frame(void)
float halfbox = BOX_SIZE / 2.0f;
// player
for (int i = 0; i < MAX_PLAYERS; i++)
{
struct Player *p = &gs.players[i];
if (!p->connected)
continue;
static float opacities[MAX_PLAYERS] = {1.0f};
opacities[i] = lerp(opacities[i], p->currently_inhabiting_index == -1 ? 1.0f : 0.1f, dt * 7.0f);
Color col_to_draw = Collerp(WHITE, GOLD, p->goldness);
col_to_draw.a = opacities[i];
set_color(col_to_draw);
sgp_push_transform();
float psize = 0.1f;
sgp_draw_filled_rect(p->pos.x - psize / 2.0f, p->pos.y - psize / 2.0f, psize, psize);
sgp_pop_transform();
// sgp_rotate_at(grid_rotation(p->grid), grid_pos(p->grid).x, grid_pos(p->grid).y);
// V2 bpos = grid_pos(p->grid);
// sgp_draw_filled_rect(grid_pos(p->grid).x - halfbox, grid_pos(p->grid).y - halfbox, BOX_SIZE, BOX_SIZE);
// sgp_pop_transform();
// sgp_set_color(1.0f, 0.0f, 0.0f, 1.0f);
// V2 vel = grid_vel(p->grid);
// V2 to = V2add(grid_pos(p->grid), vel);
// sgp_draw_line(grid_pos(p->grid).x, grid_pos(p->grid).y, to.x, to.y);
}
// mouse
if (mouse_frozen)
{
@ -461,6 +472,34 @@ static void frame(void)
}
}
// player
for (int i = 0; i < MAX_PLAYERS; i++)
{
struct Player *p = &gs.players[i];
if (!p->connected)
continue;
static float opacities[MAX_PLAYERS] = {1.0f};
opacities[i] = lerp(opacities[i], p->currently_inhabiting_index == -1 ? 1.0f : 0.1f, dt * 7.0f);
Color col_to_draw = Collerp(WHITE, GOLD, p->goldness);
col_to_draw.a = opacities[i];
set_color(col_to_draw);
sgp_push_transform();
float psize = 0.1f;
sgp_draw_filled_rect(p->pos.x - psize / 2.0f, p->pos.y - psize / 2.0f, psize, psize);
sgp_pop_transform();
// sgp_rotate_at(grid_rotation(p->grid), grid_pos(p->grid).x, grid_pos(p->grid).y);
// V2 bpos = grid_pos(p->grid);
// sgp_draw_filled_rect(grid_pos(p->grid).x - halfbox, grid_pos(p->grid).y - halfbox, BOX_SIZE, BOX_SIZE);
// sgp_pop_transform();
// sgp_set_color(1.0f, 0.0f, 0.0f, 1.0f);
// V2 vel = grid_vel(p->grid);
// V2 to = V2add(grid_pos(p->grid), vel);
// sgp_draw_line(grid_pos(p->grid).x, grid_pos(p->grid).y, to.x, to.y);
}
// gold target
set_color(GOLD);
sgp_draw_filled_rect(gs.goldpos.x, gs.goldpos.y, 0.1f, 0.1f);
@ -514,7 +553,7 @@ void event(const sapp_event *e)
}
break;
case SAPP_EVENTTYPE_MOUSE_SCROLL:
zoom_target *= 1.0f + (e->scroll_y/4.0f) * 0.1f;
zoom_target *= 1.0f + (e->scroll_y / 4.0f) * 0.1f;
zoom_target = clamp(zoom_target, 0.5f, 900.0f);
break;
case SAPP_EVENTTYPE_MOUSE_DOWN:

@ -17,16 +17,16 @@ void server(void *data)
// box haven
if (true)
{
grid_new(&gs.grids[0], &gs, (V2){.x = 0.75f, .y = 0.0});
grid_new(&gs.grids[0], &gs, (V2){.x = BOX_SIZE*3.0, .y = 0.0});
box_new(&gs.grids[0].boxes[0], &gs, &gs.grids[0], (V2){0});
box_new(&gs.grids[0].boxes[1], &gs, &gs.grids[0], (V2){0, 0.5f});
box_new(&gs.grids[0].boxes[2], &gs, &gs.grids[0], (V2){0, 1.0f});
box_new(&gs.grids[0].boxes[3], &gs, &gs.grids[0], (V2){0.5f, 1.0f});
box_new(&gs.grids[0].boxes[1], &gs, &gs.grids[0], (V2){0, BOX_SIZE});
box_new(&gs.grids[0].boxes[2], &gs, &gs.grids[0], (V2){0, BOX_SIZE*2.0});
box_new(&gs.grids[0].boxes[3], &gs, &gs.grids[0], (V2){BOX_SIZE, BOX_SIZE*2.0});
grid_new(&gs.grids[1], &gs, (V2){.x = -0.75f, .y = 0.0});
grid_new(&gs.grids[1], &gs, (V2){.x = -BOX_SIZE*1.5, .y = 0.0});
box_new(&gs.grids[1].boxes[0], &gs, &gs.grids[1], (V2){0});
grid_new(&gs.grids[2], &gs, (V2){.x = -0.75f, .y = 0.5});
grid_new(&gs.grids[2], &gs, (V2){.x = -BOX_SIZE*1.5, .y = BOX_SIZE});
box_new(&gs.grids[2].boxes[0], &gs, &gs.grids[2], (V2){0});
}

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save