-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit e033817
Showing
13 changed files
with
385 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
o/* | ||
*/*.o | ||
*.o | ||
librenderer.so.1 | ||
librenderer.a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
include config.mk | ||
|
||
all: shared static | ||
|
||
# --- librenderer library --- | ||
shared: librenderer.so.1 | ||
static: librenderer.a | ||
|
||
librenderer.o: librenderer.c | ||
$(CC) -fPIC -lm $(CFLAGS) -o librenderer.o -c librenderer.c | ||
|
||
librenderer.so.1: librenderer.o | ||
$(CC) -lm -shared $(CFLAGS) -o librenderer.so.1 librenderer.o | ||
|
||
librenderer.a: librenderer.o | ||
$(AR) rcs librenderer.a librenderer.o | ||
|
||
# --- Examples & related --- | ||
util.o: util.c | ||
$(CC) -c -lsodium $(CFLAGS) -o util.o util.c | ||
|
||
examples: o/bball o/randpix o/randline o/cosgraph | ||
|
||
examples/bball.o: examples/bball.c | ||
$(CC) -c -lsodium $(CFLAGS) -o examples/bball.o examples/bball.c | ||
|
||
o/bball: examples/bball.o util.o librenderer.a | ||
$(CC) -lsodium $(CFLAGS) -o o/bball examples/bball.o util.o librenderer.a | ||
|
||
examples/randpix.o: examples/randpix.c | ||
$(CC) -c -lsodium $(CFLAGS) -o examples/randpix.o examples/randpix.c | ||
|
||
o/randpix: examples/randpix.o util.o librenderer.a | ||
$(CC) -lsodium $(CFLAGS) -o o/randpix examples/randpix.o util.o librenderer.a | ||
|
||
examples/randline.o: examples/randline.c | ||
$(CC) -c -lsodium $(CFLAGS) -o examples/randline.o examples/randline.c | ||
|
||
o/randline: examples/randline.o util.o librenderer.a | ||
$(CC) -lsodium $(CFLAGS) -o o/randline examples/randline.o util.o librenderer.a | ||
|
||
examples/cosgraph.o: examples/cosgraph.c | ||
$(CC) -c $(CFLAGS) -o examples/cosgraph.o examples/cosgraph.c | ||
|
||
o/cosgraph: examples/cosgraph.o librenderer.a | ||
$(CC) $(CFLAGS) -o o/cosgraph examples/cosgraph.o librenderer.a -lm | ||
|
||
clean: | ||
rm -rf *.o examples/*.o *.so* *.a o/* | ||
|
||
.PHONY: clean examples shared static |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fix mkpixel x y orientation | ||
cosgraph example | ||
add to git repo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#ifndef CONFIG_H | ||
#define CONFIG_H | ||
|
||
static const int cols = 80; | ||
static const int rows = 25; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
CC = cc | ||
AR = ar | ||
CFLAGS = -O2 -std=c99 -Wall -Wextra -Wpedantic |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#define _XOPEN_SOURCE 600 | ||
#define _POSIX_C_SOURCE 200112L | ||
|
||
/// Useful includes | ||
#include <stdio.h> | ||
//#include <stdlib.h> | ||
//#include <getopt.h> | ||
//#include <unistd.h> | ||
//#include <string.h> | ||
#include "../librenderer.h" | ||
#include "../util.h" | ||
|
||
int main(void) { | ||
if(sodium_init() == -1) { | ||
fprintf(stderr, "sodium_init() failed\n"); | ||
return 2; | ||
} | ||
|
||
char pixelmap[rows][cols]; | ||
init_pxmap(pixelmap); | ||
|
||
printf("\033[s"); // save cursor position | ||
|
||
int x_pos = 12; // set initial x position | ||
int y_pos = 40; // set initial y position | ||
int speed_x = randint(-3,3); // set initial horizontal speed | ||
int speed_y = randint(-3,3); // set initial vertical speed | ||
|
||
/* TODO draw initial ball here */ | ||
add_pixel(pixelmap, x_pos, y_pos, 1.0); | ||
render_pixelmap(pixelmap); | ||
usleep(800000); | ||
|
||
while(1) { | ||
if(x_pos >= 0 || x_pos < cols || y_pos >= 0 || y_pos < rows) | ||
add_pixel(pixelmap, x_pos, y_pos, 0.0); | ||
x_pos += speed_x; y_pos += speed_y; // move | ||
if(x_pos >= 0 || x_pos < cols || y_pos >= 0 || y_pos < rows) | ||
add_pixel(pixelmap, x_pos, y_pos, 1.0); | ||
render_pixelmap(pixelmap); | ||
if(x_pos <= 0 || x_pos >= rows) | ||
speed_x = -speed_x; // bounce of the wall (horizontal) | ||
if(y_pos <= 0 || y_pos >= cols) | ||
speed_y = -speed_y; // bounce of the wall (vertical) | ||
usleep(30000); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#define _XOPEN_SOURCE 600 | ||
#define _POSIX_C_SOURCE 200112L | ||
|
||
/// Useful includes | ||
#include <stdio.h> | ||
//#include <stdlib.h> | ||
//#include <getopt.h> | ||
//#include <unistd.h> | ||
//#include <string.h> | ||
#include <math.h> | ||
#include "../librenderer.h" | ||
|
||
int main(void) { | ||
char pixelmap[rows][cols]; | ||
init_pxmap(pixelmap); | ||
|
||
printf("\033[s"); // save cursor position | ||
|
||
// TODO for every x from 0 to 'cols', graph the cos(x) | ||
|
||
add_pixel(pixelmap, 1,0,1.0f); | ||
render_pixelmap(pixelmap); | ||
|
||
/* | ||
for(int x = 0; x < cols; x++) { | ||
add_pixel(pixelmap, (int)(cos(x)*5), (int)rows/2, 1.0f); | ||
render_pixelmap(pixelmap); | ||
} | ||
*/ | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#define _XOPEN_SOURCE 600 | ||
#define _POSIX_C_SOURCE 200112L | ||
|
||
/// Useful includes | ||
#include <stdio.h> | ||
//#include <stdlib.h> | ||
//#include <getopt.h> | ||
//#include <unistd.h> | ||
//#include <string.h> | ||
#include "../librenderer.h" | ||
#include "../util.h" | ||
|
||
int main(void) { | ||
if(sodium_init() == -1) { | ||
fprintf(stderr, "sodium_init() failed\n"); | ||
return 2; | ||
} | ||
|
||
char pixelmap[rows][cols]; | ||
init_pxmap(pixelmap); | ||
|
||
printf("\033[s"); // save cursor position | ||
|
||
// TODO these lines segfault | ||
//mkline(pixelmap,15,58,4,15); | ||
//mkline(pixelmap,14,13,20,73); | ||
//mkline(pixelmap,5,29,11,66); | ||
//mkline(pixelmap,0,25,80,0); | ||
|
||
mkline(pixelmap,3,35,13,6); | ||
mkline(pixelmap,0,0,80,25); | ||
int a,b,c,d; | ||
while(1) { | ||
a = randint(1,rows-1); b = randint(1,cols-1); | ||
c = randint(1,rows-1); d = randint(1,cols-1); | ||
render_pixelmap(pixelmap); | ||
printf("\n\n (%d;%d) -> (%d;%d) \n",a,b,c,d); | ||
mkline(pixelmap,a,b,c,d); | ||
usleep(200000); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#define _XOPEN_SOURCE 600 | ||
#define _POSIX_C_SOURCE 200112L | ||
|
||
/// Useful includes | ||
#include <stdio.h> | ||
//#include <stdlib.h> | ||
//#include <getopt.h> | ||
//#include <unistd.h> | ||
//#include <string.h> | ||
#include "../librenderer.h" | ||
#include "../util.h" | ||
|
||
int main(void) { | ||
if(sodium_init() == -1) { | ||
fprintf(stderr, "sodium_init() failed\n"); | ||
return 2; | ||
} | ||
|
||
char pixelmap[rows][cols]; | ||
init_pxmap(pixelmap); | ||
|
||
printf("\033[s"); // save cursor position | ||
|
||
while(1) { | ||
render_pixelmap(pixelmap); | ||
add_pixel(pixelmap, randint(0,rows), randint(0,cols), randfloat(0.0,1.0)); | ||
usleep(20000); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
#define _POSIX_C_SOURCE 200112L | ||
|
||
#include "librenderer.h" | ||
// int cols; defined in config.h; default 80 | ||
// int rows; defined in config.h; default 25 | ||
|
||
void swap(int* a, int* b) { | ||
int tmp = *a; | ||
*a = *b; | ||
*b = tmp; | ||
} | ||
|
||
float fract_part(float x) { | ||
if(x > 0) return x - (int)x; | ||
else return x - ((int)x+1); | ||
} | ||
|
||
char opacity2ch(float opac) { | ||
// ._:-=+*n#%@ | ||
if(opac <= 0.0f) { | ||
return ' '; | ||
} else if(opac > 0.0f && opac <= 0.1f) { | ||
return '.'; | ||
} else if(opac > 0.1f && opac <= 0.2f) { | ||
return '_'; | ||
} else if(opac > 0.2f && opac <= 0.3f) { | ||
return ':'; | ||
} else if(opac > 0.3f && opac <= 0.4f) { | ||
return '-'; | ||
} else if(opac > 0.4f && opac <= 0.5f) { | ||
return '='; | ||
} else if(opac > 0.5f && opac <= 0.6f) { | ||
return '+'; | ||
} else if(opac > 0.6f && opac <= 0.7f) { | ||
return '*'; | ||
} else if(opac > 0.7f && opac <= 0.8f) { | ||
return 'n'; | ||
} else if(opac > 0.8f && opac <= 0.9f) { | ||
return '#'; | ||
} else if(opac > 0.9f && opac < 1.0f) { | ||
return '%'; | ||
} else if(opac >= 1.0f){ | ||
return '@'; | ||
} | ||
return '\0'; // default; should never run idealy | ||
} | ||
|
||
void add_pixel(char pxmap[rows][cols], int x, int y, float opac) { | ||
pxmap[x][y] = opacity2ch(opac); | ||
} | ||
|
||
void render_pixelmap(char pxmap[rows][cols]) { | ||
printf("\033[u"); // restore cursor position | ||
for(int i=0; i<rows; i++) { | ||
for(int j=0; j<cols; j++) { | ||
printf("%c", pxmap[i][j]); | ||
} | ||
printf("\n"); | ||
} | ||
} | ||
|
||
void mkline(char pxmap[rows][cols], int x0, int y0, int x1, int y1) { | ||
int steep = abs(y1-y0) > abs(x1-x0); | ||
|
||
if(steep) { | ||
swap(&x0,&y0); | ||
swap(&x1,&y1); | ||
} | ||
if(x0 > x1) { | ||
swap(&x0,&x1); | ||
swap(&y0,&y1); | ||
} | ||
|
||
float dx = x1-x0; | ||
float dy = y1-y0; | ||
float m; | ||
if(dx == 0.0f) | ||
m = 1; | ||
else | ||
m = dy/dx; | ||
|
||
int xpxl1 = x0; | ||
int xpxl2 = x1; | ||
float intersect_y = y0; | ||
|
||
steep = 1; | ||
if(steep) { | ||
int x; | ||
for(x = xpxl1; x <= xpxl2; x++) { | ||
add_pixel(pxmap, | ||
(int)intersect_y, x, | ||
1 - fract_part(intersect_y) | ||
); | ||
add_pixel(pxmap, | ||
(int)intersect_y - 1, x, | ||
fract_part(intersect_y) | ||
); | ||
intersect_y += m; | ||
} | ||
} else { // not steep | ||
int x; | ||
for(x = xpxl1; x <= xpxl2; x++) { | ||
add_pixel(pxmap, | ||
x, (int)intersect_y, | ||
1 - fract_part(intersect_y) | ||
); | ||
add_pixel(pxmap, | ||
x, (int)intersect_y - 1, | ||
fract_part(intersect_y) | ||
); | ||
intersect_y += m; | ||
} | ||
} | ||
} | ||
|
||
void init_pxmap(char pxmap[rows][cols]) { // initialize pixelmap with spaces | ||
for(int i=0; i<rows; i++) { | ||
for(int j=0; j<cols; j++) { | ||
pxmap[i][j] = ' '; | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#ifndef LIBRENDERER_H | ||
#define LIBRENDERER_H | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
#include <math.h> | ||
#include <stdint.h> | ||
|
||
#include "config.h" | ||
|
||
void swap(int* a, int* b); | ||
float fract_part(float x); | ||
char opacity2ch(float opac); | ||
void add_pixel(char pxmap[rows][cols], int x, int y, float opac); | ||
void render_pixelmap(char pxmap[rows][cols]); | ||
void mkline(char pxmap[rows][cols], int x0, int y0, int x1, int y1); | ||
void init_pxmap(char pxmap[rows][cols]); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#define _POSIX_C_SOURCE 200112L | ||
|
||
#include "util.h" | ||
|
||
uint32_t randint(uint32_t min, uint32_t max) { | ||
return randombytes_uniform(max-min) + min; | ||
} | ||
|
||
float randfloat(float min, float max) { | ||
max = max*1000; min = min*1000; | ||
return (randombytes_uniform(max-min) + min) / 1000; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#define _POSIX_C_SOURCE 200112L | ||
#ifndef UTIL_H | ||
#define UTIL_H | ||
#include <sodium.h> | ||
|
||
uint32_t randint(uint32_t min, uint32_t max); | ||
float randfloat(float min, float max); | ||
|
||
#endif |