Skip to content

Commit 2746416

Browse files
committed
Add new window to display state stats
1 parent 34b1e56 commit 2746416

File tree

4 files changed

+143
-63
lines changed

4 files changed

+143
-63
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.so
2+
*.o
3+
.vscode
4+
examples/cbrain

src/brain.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ int neuron_update(struct neuron* n, struct brain* b)
130130

131131
b->image[image_row][image_col].r = (uint8_t)(n->thisstate * 255 / THRESHOLD);
132132
b->image[image_row][image_col].g = (uint8_t)(n->nextstate * 255 / THRESHOLD);
133-
b->image[image_row][image_col].b = (uint8_t)(n->fired * 255 / THRESHOLD);
133+
b->image[image_row][image_col].b = (uint8_t)(n->fired * 255);
134134

135135
// fire neuron if thisstate exceeds THRESHOLD
136136
if (n->thisstate >= THRESHOLD) {
@@ -170,25 +170,25 @@ int neuron_update_range(uint s, uint e, struct brain* b)
170170
}
171171
}
172172

173-
if (b->active == 1) {
174-
char filename[12];
175-
sprintf(filename, "brain_%d.ppm", b->iteration);
176-
FILE *fp = fopen(filename, "wb");
177-
if (fp == NULL) {
178-
fprintf(stderr, "Error opening file for writing\n");
179-
exit(1);
180-
}
173+
// if (b->active == 1) {
174+
// char filename[12];
175+
// sprintf(filename, "brain_%d.ppm", b->iteration);
176+
// FILE *fp = fopen(filename, "wb");
177+
// if (fp == NULL) {
178+
// fprintf(stderr, "Error opening file for writing\n");
179+
// exit(1);
180+
// }
181181

182-
fprintf(fp, "P6\n%d %d\n%d\n", IMAGE_WIDTH, IMAGE_HEIGHT, MAXVAL);
182+
// fprintf(fp, "P6\n%d %d\n%d\n", IMAGE_WIDTH, IMAGE_HEIGHT, MAXVAL);
183183

184-
for (int i = 0; i < IMAGE_HEIGHT; i++) {
185-
for (int j = 0; j < IMAGE_WIDTH; j++) {
186-
fwrite(&b->image[i][j], sizeof(pixel), 1, fp);
187-
}
188-
}
184+
// for (int i = 0; i < IMAGE_HEIGHT; i++) {
185+
// for (int j = 0; j < IMAGE_WIDTH; j++) {
186+
// fwrite(&b->image[i][j], sizeof(pixel), 1, fp);
187+
// }
188+
// }
189189

190-
fclose(fp);
191-
}
190+
// fclose(fp);
191+
// }
192192

193193
b->iteration += 1;
194194
return nf;

src/cbrain.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ struct sdlctx {
102102
SDL_Window* win;
103103
SDL_Renderer* ren;
104104
SDL_Event event;
105+
106+
SDL_Window* win2;
107+
SDL_Renderer* ren2;
108+
SDL_Event event2;
105109
} sdlctx;
106110

107111
struct vect {
@@ -164,6 +168,7 @@ void render_update(struct sdlctx*, struct entityctx*, struct brain*);
164168
void render_draw(struct sdlctx*, struct entityctx*, struct brain*);
165169
void render_draw_activity_level(struct sdlctx*, struct entityctx*, struct brain*);
166170
void render_draw_brain(struct sdlctx*, struct entityctx*, struct brain*);
171+
void render_draw_activity(struct sdlctx*, struct entityctx*, struct brain*);
167172
void render_cleanup(struct sdlctx*);
168173
struct entityctx* render_spawn(int, int, int, int);
169174
#endif

src/render.c

Lines changed: 117 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -22,39 +22,55 @@ SOFTWARE.
2222

2323
int last_max_level = 0;
2424

25-
struct sdlctx* render_init()
25+
struct sdlctx *render_init()
2626
{
27-
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
27+
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
28+
{
2829
printf("SDL_Init failed: %s\n", SDL_GetError());
2930
exit(1);
30-
} else {
31-
struct sdlctx* ctx = (struct sdlctx*)malloc(sizeof(struct sdlctx));
31+
}
32+
else
33+
{
34+
struct sdlctx *ctx = (struct sdlctx *)malloc(sizeof(struct sdlctx));
3235
ctx->win = SDL_CreateWindow("cbrain", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WIN_WIDTH, WIN_HEIGHT, SDL_WINDOW_SHOWN);
3336
ctx->ren = SDL_CreateRenderer(ctx->win, -1, 0);
37+
ctx->win2 = SDL_CreateWindow("brain", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1000, 600, SDL_WINDOW_SHOWN);
38+
ctx->ren2 = SDL_CreateRenderer(ctx->win2, -1, 0);
3439
return ctx;
3540
}
3641
}
3742

38-
void render_handle_events(struct sdlctx* ctx)
43+
void render_handle_events(struct sdlctx *ctx)
3944
{
4045
SDL_PollEvent(&ctx->event);
41-
switch (ctx->event.type) {
42-
case SDL_QUIT:
43-
render_cleanup(ctx);
44-
break;
46+
switch (ctx->event.type)
47+
{
48+
case SDL_QUIT:
49+
render_cleanup(ctx);
50+
break;
51+
}
52+
53+
SDL_PollEvent(&ctx->event2);
54+
switch (ctx->event2.type)
55+
{
56+
case SDL_QUIT:
57+
render_cleanup(ctx);
58+
break;
4559
}
4660
}
4761

48-
void render_update(struct sdlctx* ctx, struct entityctx* ec, struct brain* b)
62+
void render_update(struct sdlctx *ctx, struct entityctx *ec, struct brain *b)
4963
{
5064
int acc_right = 0;
5165
int acc_left = 0;
52-
for (int i = ec->mrstart; i < ec->mrend; i++) {
66+
for (int i = ec->mrstart; i < ec->mrend; i++)
67+
{
5368
acc_right += b->neurons[i]->thisstate;
5469
b->neurons[i]->thisstate *= 0.7;
5570
}
5671

57-
for (int i = ec->mlstart; i < ec->mlend; i++) {
72+
for (int i = ec->mlstart; i < ec->mlend; i++)
73+
{
5874
int st = b->neurons[i]->thisstate;
5975
acc_left += st;
6076
b->neurons[i]->thisstate *= 0.7;
@@ -63,12 +79,13 @@ void render_update(struct sdlctx* ctx, struct entityctx* ec, struct brain* b)
6379
float speed = acc_right + acc_left;
6480
speed = speed / 150.0;
6581

66-
ec->rot += (acc_right > acc_left) ? -5.0 : (acc_left > acc_right) ? 5.0 : 0.0;
82+
ec->rot += (acc_right > acc_left) ? -5.0 : (acc_left > acc_right) ? 5.0
83+
: 0.0;
6784
ec->x += cosf(ec->rot / RADTODEG) * speed;
6885
ec->y += sinf(ec->rot / RADTODEG) * speed;
6986
}
7087

71-
void render_draw(struct sdlctx* ctx, struct entityctx* ec, struct brain* b)
88+
void render_draw(struct sdlctx *ctx, struct entityctx *ec, struct brain *b)
7289
{
7390
float v1x = 0.0f;
7491
float v2x = 0.0f;
@@ -79,7 +96,8 @@ void render_draw(struct sdlctx* ctx, struct entityctx* ec, struct brain* b)
7996
float v3y = 0.0f;
8097
float v4y = 0.0f;
8198

82-
if (ec->rot != 0) {
99+
if (ec->rot != 0)
100+
{
83101
float ca = cosf(ec->rot * 3.14159 / 180);
84102
float sa = sinf(ec->rot * 3.14159 / 180);
85103

@@ -91,15 +109,17 @@ void render_draw(struct sdlctx* ctx, struct entityctx* ec, struct brain* b)
91109
float w = ec->x + ec->width - cx;
92110
float h = ec->y + ec->height - cy;
93111

94-
v1x = x*ca - y*sa + cx;
95-
v1y = x*sa + y*ca + cy;
96-
v2x = w*ca - y*sa + cx;
97-
v2y = w*sa + y*ca + cy;
98-
v3x = w*ca - h*sa + cx;
99-
v3y = w*sa + h*ca + cy;
100-
v4x = x*ca - h*sa + cx;
101-
v4y = x*sa + h*ca + cy;
102-
} else {
112+
v1x = x * ca - y * sa + cx;
113+
v1y = x * sa + y * ca + cy;
114+
v2x = w * ca - y * sa + cx;
115+
v2y = w * sa + y * ca + cy;
116+
v3x = w * ca - h * sa + cx;
117+
v3y = w * sa + h * ca + cy;
118+
v4x = x * ca - h * sa + cx;
119+
v4y = x * sa + h * ca + cy;
120+
}
121+
else
122+
{
103123
v1x = ec->x - 20;
104124
v1y = ec->y - 10;
105125
v2x = ec->x + ec->width - 20;
@@ -112,57 +132,102 @@ void render_draw(struct sdlctx* ctx, struct entityctx* ec, struct brain* b)
112132

113133
SDL_SetRenderDrawColor(ctx->ren, 255, 255, 255, 255);
114134
SDL_RenderClear(ctx->ren);
135+
SDL_SetRenderDrawColor(ctx->ren2, 255, 255, 255, 255);
136+
SDL_RenderClear(ctx->ren2);
115137
render_draw_brain(ctx, ec, b);
138+
render_draw_activity(ctx, ec, b);
116139
render_draw_activity_level(ctx, ec, b);
117140
SDL_SetRenderDrawColor(ctx->ren, 0, 0, 0, 255);
118141
SDL_RenderDrawLine(ctx->ren, v4x, v4y, v1x, v1y);
119142
SDL_RenderDrawLine(ctx->ren, v1x, v1y, v2x, v2y);
120143
SDL_RenderDrawLine(ctx->ren, v2x, v2y, v3x, v3y);
121144
SDL_RenderDrawLine(ctx->ren, v3x, v3y, v4x, v4y);
122145
SDL_RenderPresent(ctx->ren);
146+
SDL_RenderPresent(ctx->ren2);
147+
}
148+
149+
void render_draw_activity(struct sdlctx *ctx, struct entityctx *ec, struct brain *b)
150+
{
151+
int cols = 32; // number of columns in the grid
152+
int tileSize = 32; // width/height of each cell in pixels
153+
for (int i = 0; i < b->nc; i++)
154+
{
155+
156+
int x = i % cols; // column
157+
int y = i / cols; // row
158+
159+
SDL_Rect rect;
160+
rect.x = x * tileSize;
161+
rect.y = y * tileSize;
162+
rect.w = tileSize;
163+
rect.h = tileSize;
164+
165+
int image_row = i / IMAGE_WIDTH;
166+
int image_col = i % IMAGE_WIDTH;
167+
168+
int red = b->image[image_row][image_col].r;
169+
int green = b->image[image_row][image_col].g;
170+
int blue = b->image[image_row][image_col].b;
171+
SDL_SetRenderDrawColor(ctx->ren2, red, green, blue, 255);
172+
if (SDL_RenderFillRect(ctx->ren2, &rect) != 0)
173+
{
174+
printf("SDL_RenderDrawRect() for %d: %s", i, SDL_GetError());
175+
}
176+
}
177+
SDL_SetRenderDrawColor(ctx->ren2, 0, 0, 0, 255);
123178
}
124179

125-
void render_draw_brain(struct sdlctx* ctx, struct entityctx* ec, struct brain* b)
180+
void render_draw_brain(struct sdlctx *ctx, struct entityctx *ec, struct brain *b)
126181
{
127-
for (int i = 0; i < b->nc; i++) {
182+
for (int i = 0; i < b->nc; i++)
183+
{
128184
int x = i % 16;
129185
int y = i / 16.0;
130186
SDL_Rect rect;
131187
rect.x = x * 8;
132188
rect.y = y * 8;
133189
rect.w = 8;
134190
rect.h = 8;
135-
if (b->neurons[i]->fired) {
136-
if (b->neurons[i]->f_type == user) {
191+
if (b->neurons[i]->fired)
192+
{
193+
if (b->neurons[i]->f_type == user)
194+
{
137195
SDL_SetRenderDrawColor(ctx->ren, 223, 155, 109, 255);
138-
} else {
196+
}
197+
else
198+
{
139199
SDL_SetRenderDrawColor(ctx->ren, 175, 27, 63, 255);
140200
}
141-
if (SDL_RenderFillRect(ctx->ren, &rect) != 0) {
201+
if (SDL_RenderFillRect(ctx->ren, &rect) != 0)
202+
{
142203
printf("SDL_RenderDrawRect() for %d: %s", i, SDL_GetError());
143204
}
144205
}
145-
else {
146-
if (b->neurons[i]->thisstate == 0) {
206+
else
207+
{
208+
if (b->neurons[i]->thisstate == 0)
209+
{
147210
SDL_SetRenderDrawColor(ctx->ren, 71, 49, 68, 255);
148-
if (SDL_RenderFillRect(ctx->ren, &rect) != 0) {
211+
if (SDL_RenderFillRect(ctx->ren, &rect) != 0)
212+
{
149213
printf("SDL_RenderDrawRect() for %d: %s", i, SDL_GetError());
150214
}
151215
}
152-
else {
216+
else
217+
{
153218
// int shade = 255 - (255 * b->neurons[i]->thisstate / THRESHOLD);
154219
// SDL_SetRenderDrawColor(ctx->ren, shade, shade, shade, 255);
155220
SDL_SetRenderDrawColor(ctx->ren, 150, 150, 150, 255);
156-
//if (SDL_RenderDrawRect(ctx->ren, &rect) != 0) {
221+
// if (SDL_RenderDrawRect(ctx->ren, &rect) != 0) {
157222
// printf("SDL_RenderDrawRect() for %d: %s", i, SDL_GetError());
158-
//}
223+
// }
159224
}
160225
}
161226
}
162227
SDL_SetRenderDrawColor(ctx->ren, 0, 0, 0, 255);
163228
}
164229

165-
void render_draw_activity_level(struct sdlctx* ctx, struct entityctx* ec, struct brain* b)
230+
void render_draw_activity_level(struct sdlctx *ctx, struct entityctx *ec, struct brain *b)
166231
{
167232
int activity_box_height = 16;
168233
SDL_Rect rect;
@@ -171,18 +236,22 @@ void render_draw_activity_level(struct sdlctx* ctx, struct entityctx* ec, struct
171236
rect.w = WIN_WIDTH;
172237
rect.h = activity_box_height;
173238
SDL_SetRenderDrawColor(ctx->ren, 100, 100, 100, 255);
174-
if (SDL_RenderDrawRect(ctx->ren, &rect) != 0) {
239+
if (SDL_RenderDrawRect(ctx->ren, &rect) != 0)
240+
{
175241
printf("SDL_RenderDrawRect() for activity level: %s", SDL_GetError());
176242
}
177243

178244
int total_fired = 0;
179-
for (int i = 0; i < b->nc; i++) {
180-
if (b->neurons[i]->fired) {
245+
for (int i = 0; i < b->nc; i++)
246+
{
247+
if (b->neurons[i]->fired)
248+
{
181249
total_fired++;
182250
}
183251
}
184252

185-
if (last_max_level < total_fired) {
253+
if (last_max_level < total_fired)
254+
{
186255
last_max_level = total_fired;
187256
}
188257

@@ -192,27 +261,29 @@ void render_draw_activity_level(struct sdlctx* ctx, struct entityctx* ec, struct
192261
acrect.y = WIN_HEIGHT - activity_box_height;
193262
acrect.w = wf;
194263
acrect.h = activity_box_height;
195-
if (SDL_RenderFillRect(ctx->ren, &acrect) != 0) {
264+
if (SDL_RenderFillRect(ctx->ren, &acrect) != 0)
265+
{
196266
printf("SDL_RenderDrawRect() for activity level: %s", SDL_GetError());
197267
}
198268

199269
SDL_RenderDrawLine(ctx->ren, wf, acrect.y - 4, wf, WIN_HEIGHT);
200270
SDL_RenderDrawLine(ctx->ren, WIN_WIDTH * last_max_level / b->nc, WIN_HEIGHT - activity_box_height, WIN_WIDTH * last_max_level / b->nc, WIN_HEIGHT);
201-
202271
}
203272

204-
void render_cleanup(struct sdlctx* ctx)
273+
void render_cleanup(struct sdlctx *ctx)
205274
{
206275
printf("Quitting...\n");
207276
SDL_DestroyWindow(ctx->win);
208277
SDL_DestroyRenderer(ctx->ren);
278+
SDL_DestroyWindow(ctx->win2);
279+
SDL_DestroyRenderer(ctx->ren2);
209280
SDL_Quit();
210281
exit(1);
211282
}
212283

213-
struct entityctx* render_spawn(int mls, int mrs, int mle, int mre)
284+
struct entityctx *render_spawn(int mls, int mrs, int mle, int mre)
214285
{
215-
struct entityctx* ec = (struct entityctx*)malloc(sizeof(struct entityctx));
286+
struct entityctx *ec = (struct entityctx *)malloc(sizeof(struct entityctx));
216287
ec->x = 300;
217288
ec->y = 200;
218289
ec->rot = 0.0f;

0 commit comments

Comments
 (0)