Skip to content

Commit

Permalink
Add linter and formatter (#1)
Browse files Browse the repository at this point in the history
* refeactored makefile

* fix import order to pass lint

* add docker var to makefile for sudo or not

* add linter config

* format code

* makefile runs docker as a user

* fix lint errors

Co-authored-by: tommarks <[email protected]>
Co-authored-by: Tom Marks <[email protected]>
  • Loading branch information
3 people authored Aug 29, 2021
1 parent 971d1ad commit 22acb1a
Show file tree
Hide file tree
Showing 13 changed files with 163 additions and 178 deletions.
3 changes: 3 additions & 0 deletions .lintvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CPPLINT_FILTERS=-readability/casting,-build/include_subdir,-legal,-build/header_guard,-whitespace/blank_line
CPPLINT_LINE_LENGTH=80

24 changes: 17 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
PS2SDK=/usr/local/ps2dev/ps2sdk

ISO_TGT=test.iso
EE_BIN=test.elf
EE_OBJS=main.o gs.o mesh.o draw.o math.o pad.o

EE_LIBS=-ldma -lgraph -ldraw -lkernel -ldebug -lmath3d -lm -lpad

EE_CFLAGS += -Wall --std=c99
EE_LDFLAGS = -L$(PSDSDK)/ee/common/lib -L$(PS2SDK)/ee/lib


PS2SDK=/usr/local/ps2dev/ps2sdk

PS2HOST?=192.168.20.99

ISO_TGT=test.iso
DOCKER_IMG=ps2build
DOCKERFLAGS=--user "$(shell id -u):$(shell id -g)"
DOCKER?=sudo docker

include .lintvars

ifdef PLATFORM
include $(PS2SDK)/samples/Makefile.eeglobal
Expand All @@ -24,7 +25,7 @@ $(ISO_TGT): $(EE_BIN)

.PHONY: docker-build
docker-build:
docker run -v $(shell pwd):/src ps2build make $(ISO_TGT)
$(DOCKER) run -v $(shell pwd):/src $(DOCKER_IMG) make $(ISO_TGT)


.PHONY: clean
Expand All @@ -42,3 +43,12 @@ runps2:
.PHONY: resetps2
resetps2:
ps2client -h $(PS2HOST) -t 5 reset

.PHONY: lint
lint:
cpplint --filter=$(CPPLINT_FILTERS) --counting=total --linelength=$(CPPLINT_LINE_LENGTH) --extensions=c,h *.c *.h

.PHONY: format
format:
$(DOCKER) run $(DOCKERFLAGS) -v $(shell pwd):/workdir unibeautify/clang-format -i -sort-includes *.c *.h

68 changes: 33 additions & 35 deletions draw.c
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@

#include <math3d.h>
#include <draw_types.h>
#include <math3d.h>

#include <stdint.h>

#include "draw.h"
#include "mesh.h"
#include "log.h"
#include "mesh.h"
#include "ps2math.h"

#define ZMAX (1024*1024)
#define ZMAX (1024 * 1024)

static int cc = 0;

void log_matrix(MATRIX m)
{
void log_matrix(MATRIX m) {
printf("Matrix = \n");
for(int i = 0; i < 4; i++) {
int b = i*4;
printf("%.2f %.2f %.2f %.2f\n", m[b], m[b+1], m[b+2], m[b+3]);
for (int i = 0; i < 4; i++) {
int b = i * 4;
printf("%.2f %.2f %.2f %.2f\n", m[b], m[b + 1], m[b + 2], m[b + 3]);
}
}

void mesh_transform(char *b, struct model_instance *inst, struct render_state *d)
{
void mesh_transform(char *b, struct model_instance *inst,
struct render_state *d) {
MATRIX tmp;
MATRIX model;
matrix_unit(model);
Expand All @@ -41,64 +40,63 @@ void mesh_transform(char *b, struct model_instance *inst, struct render_state *d
float d_avg = 0;
for (int i = 0; i < inst->m->vertex_count; i++) {
// get address of current vertex data
float *pos = (float*) (b + (stride*i) + (inst->m->vertex_position_offset*16));
float *pos =
(float *)(b + (stride * i) + (inst->m->vertex_position_offset * 16));
VECTOR *v = pos;

vector_apply(v, v, tmp);
pos[0] = pos[0]/pos[3];
pos[1] = pos[1]/pos[3];
pos[2] = pos[2]/pos[3];
pos[0] = pos[0] / pos[3];
pos[1] = pos[1] / pos[3];
pos[2] = pos[2] / pos[3];
d_avg += pos[2];

*((uint32_t*)pos) = ftoi4(pos[0]+d->offset_x);
*((uint32_t*)(pos+1)) = ftoi4(pos[1]+d->offset_y);
uint32_t zv = (uint32_t) (ZMAX * (pos[2] / 100.f));
*((uint32_t*)(pos+2)) = zv;

*((uint32_t *)pos) = ftoi4(pos[0] + d->offset_x);
*((uint32_t *)(pos + 1)) = ftoi4(pos[1] + d->offset_y);
uint32_t zv = (uint32_t)(ZMAX * (pos[2] / 100.f));
*((uint32_t *)(pos + 2)) = zv;

uint32_t * col = (uint32_t*) (b + (stride*i) + (inst->m->vertex_colour_offset*16));
uint32_t *col =
(uint32_t *)(b + (stride * i) + (inst->m->vertex_colour_offset * 16));
col[1] = 0x0f;
col[2] = 0x0f;
col[0] = (int) (((zv)/(ZMAX*1.0f)) * 255.0f);
col[0] = (int)(((zv) / (ZMAX * 1.0f)) * 255.0f);
col[3] = 0x80;
/*
*((uint32_t*)pos) = (short)((pos[0]+1.0f)*d->offset_x);
*((uint32_t*)(pos+1)) = (short)((pos[1]+1.0f)*d->offset_y);
*((uint32_t*)(pos+2)) = (unsigned int)((pos[2]+1.0f)*20);
*/
*((uint32_t*)pos) = (short)((pos[0]+1.0f)*d->offset_x);
*((uint32_t*)(pos+1)) = (short)((pos[1]+1.0f)*d->offset_y);
*((uint32_t*)(pos+2)) = (unsigned int)((pos[2]+1.0f)*20);
*/

pos[3] = 0;
}
// info("avg depth = %f", d_avg / (1.0f * inst->m->vertex_count));
cc++;
}

void create_model_matrix(MATRIX tgt, VECTOR translate, VECTOR scale, VECTOR rotate)
{
void create_model_matrix(MATRIX tgt, VECTOR translate, VECTOR scale,
VECTOR rotate) {
matrix_unit(tgt);
matrix_rotate(tgt, tgt, rotate);
matrix_scale(tgt, tgt, scale);
matrix_translate(tgt, tgt, translate);
}

void update_draw_matrix(struct render_state *d)
{
void update_draw_matrix(struct render_state *d) {
d->up[0] = 0;
d->up[1] = 1.0f;
d->up[2] = 0;
d->up[3] = 0;
VECTOR camfwd = {0,0,-1,0};

VECTOR camfwd = {0, 0, -1, 0};
vector_rotate_y(camfwd, d->camera_rotate_y);
//d->camera_tgt[0] = d->camera_pos[0] + camfwd[0];
//d->camera_tgt[1] = d->camera_pos[1] + camfwd[1];
// d->camera_tgt[0] = d->camera_pos[0] + camfwd[0];
// d->camera_tgt[1] = d->camera_pos[1] + camfwd[1];
d->camera_tgt[2] = d->camera_pos[2] - 1;

MATRIX viewport, proj, cam;
matrix_viewport(viewport, 640.f, 480.f);
matrix_proj(proj, 1.2f, 3.f/4.f, 1.f, 100.f);
matrix_proj(proj, 1.2f, 3.f / 4.f, 1.f, 100.f);
matrix_lookat(cam, d->camera_pos, d->camera_tgt, d->up);
matrix_multiply(d->world_to_screen, viewport, proj);
matrix_multiply(d->world_to_screen, d->world_to_screen, cam);
}

9 changes: 5 additions & 4 deletions draw.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#define DRAW_H

#define MESH_HEADER_SIZE 3
#define myftoi4(x) (((uint64_t)(x))<<4)
#define myftoi4(x) (((uint64_t)(x)) << 4)

struct render_state {
float offset_x;
Expand All @@ -25,9 +25,10 @@ struct model_instance {
VECTOR rotate;
};


void mesh_transform(char *b, struct model_instance *inst, struct render_state *d);
void create_model_matrix(MATRIX tgt, VECTOR translate, VECTOR scale, VECTOR rotate);
void mesh_transform(char *b, struct model_instance *inst,
struct render_state *d);
void create_model_matrix(MATRIX tgt, VECTOR translate, VECTOR scale,
VECTOR rotate);
void update_draw_matrix(struct render_state *d);

#endif
24 changes: 12 additions & 12 deletions gs.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,35 @@
#include <draw.h>
#include <graph.h>

#include <string.h>
#include <stdlib.h>

#include <string.h>

#define GS_OFFSET_X 2048
#define GS_OFFSET_Y 2048

#define GS_CMD_BUFFER_LEN (40*16)
#define GS_CMD_BUFFER_LEN (40 * 16)
static qword_t *gs_cmd_buffer;

int gs_init(struct draw_state *ds, int psm, int psmz)
{
int gs_init(struct draw_state *ds, int psm, int psmz) {
if (!gs_cmd_buffer) {
gs_cmd_buffer = malloc(GS_CMD_BUFFER_LEN);
}

ds->fb.address = graph_vram_allocate(ds->width, ds->height, psm, GRAPH_ALIGN_PAGE);
ds->fb.address =
graph_vram_allocate(ds->width, ds->height, psm, GRAPH_ALIGN_PAGE);
ds->fb.width = ds->width;
ds->fb.height = ds->height;
ds->fb.psm = psm;
ds->fb.mask = 0;

ds->zb.address = graph_vram_allocate(ds->width, ds->height, psmz, GRAPH_ALIGN_PAGE);
ds->zb.address =
graph_vram_allocate(ds->width, ds->height, psmz, GRAPH_ALIGN_PAGE);
ds->zb.enable = 1;
ds->zb.method = ZTEST_METHOD_GREATER;
ds->zb.zsm = psmz;
ds->zb.mask = 0;

graph_set_mode(ds->gmode, ds->vmode, GRAPH_MODE_FIELD, GRAPH_DISABLE);
graph_set_mode(ds->gmode, ds->vmode, GRAPH_MODE_FIELD, GRAPH_DISABLE);
graph_set_screen(0, 0, ds->width, ds->height);
graph_set_bgcolor(0, 0, 0);
graph_set_framebuffer_filtered(ds->fb.address, ds->width, psm, 0, 0);
Expand All @@ -46,12 +46,12 @@ int gs_init(struct draw_state *ds, int psm, int psmz)
qword_t *q = gs_cmd_buffer;
memset(gs_cmd_buffer, 0, GS_CMD_BUFFER_LEN);
q = draw_setup_environment(q, 0, &ds->fb, &ds->zb);
q = draw_primitive_xyoffset(q, 0, GS_OFFSET_X-(ds->width/2), GS_OFFSET_Y-(ds->height/2));
q = draw_primitive_xyoffset(q, 0, GS_OFFSET_X - (ds->width / 2),
GS_OFFSET_Y - (ds->height / 2));
q = draw_finish(q);
dma_channel_send_normal(DMA_CHANNEL_GIF, gs_cmd_buffer, q-gs_cmd_buffer, 0, 0);
dma_channel_send_normal(DMA_CHANNEL_GIF, gs_cmd_buffer, q - gs_cmd_buffer, 0,
0);
draw_wait_finish();

return 0;
}


2 changes: 1 addition & 1 deletion gs.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef GS_H
#define GS_H

#include <graph.h>
#include <draw.h>
#include <graph.h>

struct draw_state {
int width;
Expand Down
4 changes: 2 additions & 2 deletions log.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
#include <stdio.h>

#if 1
#define logmsg(lvl, msg, ...) printf(lvl " @ (%s:%d) " msg "\n", __FILE__, __LINE__, ##__VA_ARGS__)
#define logmsg(lvl, msg, ...) \
printf(lvl " @ (%s:%d) " msg "\n", __FILE__, __LINE__, ##__VA_ARGS__)
#else
#define logmsg(lvl, msg, ...) ((void)0)
#endif
Expand All @@ -13,5 +14,4 @@
#define info(msg, ...) logmsg("[INFO]", msg, ##__VA_ARGS__)
#define logerr(msg, ...) logmsg("[ERRO]", msg, ##__VA_ARGS__)


#endif
Loading

0 comments on commit 22acb1a

Please sign in to comment.