Browse Source

initial commit

master
mortie 7 years ago
commit
1d2ba83b9e
6 changed files with 165 additions and 0 deletions
  1. 1
    0
      .gitignore
  2. 5
    0
      Makefile
  3. 20
    0
      src/arr.h
  4. 50
    0
      src/creature.c
  5. 24
    0
      src/creature.h
  6. 65
    0
      src/main.c

+ 1
- 0
.gitignore View File

@@ -0,0 +1 @@
evosim

+ 5
- 0
Makefile View File

@@ -0,0 +1,5 @@
CFILES=$(shell find src -name '*.c')
CFLAGS=-std=c99 $(shell sdl2-config --cflags --libs)

build:
gcc $(CFLAGS) -o evosim $(CFILES)

+ 20
- 0
src/arr.h View File

@@ -0,0 +1,20 @@
#include <stdlib.h>

#define ARR_REALLOC(v, c, a) \
do { \
if (v == NULL) { \
a = 8; \
v = malloc(8 * sizeof(*(v))); \
} \
else if (c >= a) { \
do { a *= 2; } while (c >= a); \
v = realloc(v, a * sizeof(*(v))); \
} \
} while (0)

#define ARR_APPEND(v, c, a, nw) \
do { \
c += 1; \
ARR_REALLOC(v, c, a); \
v[c - 1] = nw; \
} while (0)

+ 50
- 0
src/creature.c View File

@@ -0,0 +1,50 @@
#include "creature.h"
#include <stdio.h>

void creature_print(creature *creat)
{
printf("Creature: %i muscles\n", creat->musclec);
for (int i = 0; i < creat->musclec; ++i)
{
printf("\tMuscle %i: \n", i);
printf("\t\tmultiplier: %f\n", creat->muscles[i].multiplier);
printf("\t\tstrength: %i\n", creat->muscles[i].strength);

printf("\t\tnode 1: ");
node n = creat->muscles[i].n1;
printf("friction: %i, x: %f, y: %f\n", n.friction, n.x, n.y);

printf("\t\tnode 2: ");
n = creat->muscles[i].n2;
printf("friction: %i, x: %f, y: %f\n", n.friction, n.x, n.y);
}
}

static void drawnode(node n, SDL_Renderer *renderer)
{
int w = 30;
int h = 30;
SDL_Rect rect = { n.x - (w / 2), n.y - (h / 2), w, h };
int col = (n.friction / 1000.0) * 0xff;
SDL_SetRenderDrawColor(renderer, 0xaa, col, col, 0xff);
SDL_RenderFillRect(renderer, &rect);
}

void creature_draw(creature *creat, SDL_Renderer *renderer)
{
for (int i = 0; i < creat->musclec; ++i)
{
muscle mus = creat->muscles[i];

// Draw muscle line
int col = (mus.strength / 1000.0) * 0xff;
SDL_SetRenderDrawColor(renderer, 0xaa, 0xff, 0xaa, col);
SDL_RenderDrawLine(renderer,
mus.n1.x, mus.n1.y,
mus.n2.x, mus.n2.y);

// Draw nodes
drawnode(mus.n1, renderer);
drawnode(mus.n2, renderer);
}
}

+ 24
- 0
src/creature.h View File

@@ -0,0 +1,24 @@
#include <SDL2/SDL.h>

typedef struct node
{
int friction; // 0 to 1000
double x;
double y;
} node;

typedef struct muscle
{
node n1;
node n2;
double multiplier;
int strength; // 0 to 1000
} muscle;

typedef struct creature
{
muscle muscles[32];
int musclec;
} creature;

void creature_draw(creature *creat, SDL_Renderer *renderer);

+ 65
- 0
src/main.c View File

@@ -0,0 +1,65 @@
#include <SDL2/SDL.h>
#include "creature.h"

int main()
{
// Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) != 0)
{
fprintf(stderr, "SDL_Init error: %s\n", SDL_GetError());
return 1;
}

// Create a window
SDL_Window *win = SDL_CreateWindow(
"BestGame",
100, 100,
640, 480,
SDL_WINDOW_SHOWN);
if (win == NULL)
{
fprintf(stderr, "SDL_CreateWindow error: %s\n", SDL_GetError());
SDL_Quit();
return 1;
}

// Get surface and renderer
SDL_Surface *surface = SDL_GetWindowSurface(win);
SDL_Renderer *renderer = SDL_CreateRenderer(
win, -1, SDL_RENDERER_ACCELERATED);

// Create a creature
creature creat = {
.muscles = {
{
.n1 = { 10, 200, 200 },
.n2 = { 10, 190, 300 },
.multiplier = 1,
.strength = 1
}
},
.musclec = 1
};

int fps = 30;
int delay = 1000 / fps;

for (int i = 0; i < 100; ++i)
{
// Clear screen
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0xff);
SDL_RenderClear(renderer);

// Draw creature
creat.muscles[0].n1.x += 1;
creature_draw(&creat, renderer);

// Presnent and delay
SDL_RenderPresent(renderer);
SDL_Delay(delay);
}

SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}

Loading…
Cancel
Save