game progress
This commit is contained in:
parent
36fd03a4ab
commit
58131cd720
@ -1,12 +1,12 @@
|
||||
CC = i686-pc-toaru-gcc
|
||||
CFLAGS = -march=core2 -std=c99 -O3 -I/afs/acm.uiuc.edu/user/lange7/toaru-toolchain/local/i686-pc-toaru/include/freetype2
|
||||
CFLAGS = -march=core2 -std=c99 -O3
|
||||
|
||||
.PHONY: all
|
||||
|
||||
all: ../initrd/bin/game ../initrd/bin/julia
|
||||
|
||||
../initrd/bin/game: main.c
|
||||
${CC} ${CFLAGS} -o $@ $< ~/toaru-toolchain/local/i686-pc-toaru/lib/libfreetype.a
|
||||
${CC} ${CFLAGS} -o $@ $<
|
||||
|
||||
../initrd/bin/julia: julia.c
|
||||
${CC} ${CFLAGS} -o $@ $<
|
||||
|
67
game/julia.c
67
game/julia.c
@ -82,49 +82,52 @@ void julia(int xpt, int ypt) {
|
||||
break;
|
||||
}
|
||||
|
||||
int color = k;
|
||||
if (!no_repeat) {
|
||||
if (color > 11) color = color % 12;
|
||||
} else {
|
||||
int color;
|
||||
if (no_repeat) {
|
||||
color = 12 * k / initer;
|
||||
} else {
|
||||
color = k;
|
||||
if (color > 11) {
|
||||
color = color % 12;
|
||||
}
|
||||
}
|
||||
if (k >= initer)
|
||||
if (k >= initer) {
|
||||
GFX(xpt, ypt) = 0;
|
||||
else
|
||||
} else {
|
||||
GFX(xpt, ypt) = colors[color];
|
||||
}
|
||||
newcolor = color;
|
||||
}
|
||||
|
||||
int main(int argc, char ** argv) {
|
||||
gfx_mem = (void *)syscall_getgraphicsaddress();
|
||||
|
||||
/* Read some arguments */
|
||||
if (argc > 1) {
|
||||
optind = 0;
|
||||
}
|
||||
int index, c;
|
||||
while ((c = getopt(argc, argv, "ni:x:X:c:C:")) != -1) {
|
||||
switch (c) {
|
||||
case 'n':
|
||||
no_repeat = 1;
|
||||
break;
|
||||
case 'i':
|
||||
initer = atof(optarg);
|
||||
break;
|
||||
case 'x':
|
||||
Minx = atof(optarg);
|
||||
break;
|
||||
case 'X':
|
||||
Maxx = atof(optarg);
|
||||
break;
|
||||
case 'c':
|
||||
conx = atof(optarg);
|
||||
break;
|
||||
case 'C':
|
||||
cony = atof(optarg);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
/* Read some arguments */
|
||||
int index, c;
|
||||
while ((c = getopt(argc, argv, "ni:x:X:c:C:")) != -1) {
|
||||
switch (c) {
|
||||
case 'n':
|
||||
no_repeat = 1;
|
||||
break;
|
||||
case 'i':
|
||||
initer = atof(optarg);
|
||||
break;
|
||||
case 'x':
|
||||
Minx = atof(optarg);
|
||||
break;
|
||||
case 'X':
|
||||
Maxx = atof(optarg);
|
||||
break;
|
||||
case 'c':
|
||||
conx = atof(optarg);
|
||||
break;
|
||||
case 'C':
|
||||
cony = atof(optarg);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("initer: %f\n", initer);
|
||||
|
172
game/main.c
172
game/main.c
@ -9,15 +9,6 @@
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#if 1
|
||||
#include <ft2build.h>
|
||||
#include FT_FREETYPE_H
|
||||
|
||||
|
||||
FT_Library library;
|
||||
FT_Face face;
|
||||
#endif
|
||||
|
||||
DEFN_SYSCALL0(getgraphicsaddress, 11);
|
||||
DEFN_SYSCALL1(kbd_mode, 12, int);
|
||||
DEFN_SYSCALL0(kbd_get, 13);
|
||||
@ -27,7 +18,9 @@ typedef struct sprite {
|
||||
uint16_t width;
|
||||
uint16_t height;
|
||||
uint32_t * bitmap;
|
||||
uint32_t * masks;
|
||||
uint32_t blank;
|
||||
uint8_t alpha;
|
||||
} sprite_t;
|
||||
|
||||
#define GFX_W 1024
|
||||
@ -35,6 +28,7 @@ typedef struct sprite {
|
||||
#define GFX_B 4
|
||||
#define GFX(x,y) frame_mem[GFX_W * (y) + (x)]
|
||||
#define SPRITE(sprite,x,y) sprite->bitmap[sprite->width * (y) + (x)]
|
||||
#define SMASKS(sprite,x,y) sprite->masks[sprite->width * (y) + (x)]
|
||||
|
||||
uint32_t * gfx_mem;
|
||||
uint32_t * frame_mem;
|
||||
@ -42,6 +36,18 @@ uint32_t gfx_size = GFX_B * GFX_H * GFX_W;
|
||||
sprite_t * sprites[128];
|
||||
|
||||
|
||||
void * malloc_(size_t size) {
|
||||
void * ret = malloc(size);
|
||||
if (!ret) {
|
||||
printf("[WARNING!] malloc_(%d) returned NULL!\n", size);
|
||||
while ((ret = malloc(size)) == NULL) {
|
||||
printf(".");
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
uint32_t rgb(uint8_t r, uint8_t g, uint8_t b) {
|
||||
return (r * 0x10000) + (g * 0x100) + (b * 0x1);
|
||||
}
|
||||
@ -71,30 +77,38 @@ load_sprite(sprite_t * sprite, char * filename) {
|
||||
fseek(image, 0, SEEK_SET);
|
||||
|
||||
/* Alright, we have the length */
|
||||
char * bufferb = malloc(image_size);
|
||||
char * bufferb = malloc_(image_size);
|
||||
fread(bufferb, image_size, 1, image);
|
||||
uint16_t x = 0; /* -> 212 */
|
||||
uint16_t y = 0; /* -> 68 */
|
||||
/* Get the width / height of the image */
|
||||
signed int *bufferi = (signed int *)((uintptr_t)bufferb + 2);
|
||||
uint32_t width = bufferi[4];
|
||||
uint32_t width = bufferi[4];
|
||||
uint32_t height = bufferi[5];
|
||||
uint32_t row_width = (24 * width + 31) / 32 * 4;
|
||||
uint16_t bpp = bufferi[6] / 0x10000;
|
||||
uint32_t row_width = (bpp * width + 31) / 32 * 4;
|
||||
/* Skip right to the important part */
|
||||
size_t i = bufferi[2];
|
||||
|
||||
sprite->width = width;
|
||||
sprite->height = height;
|
||||
sprite->bitmap = malloc(sizeof(uint32_t) * width * height);
|
||||
printf("%d x %d\n", width, height);
|
||||
sprite->bitmap = malloc_(sizeof(uint32_t) * width * height);
|
||||
|
||||
for (y = 0; y < height; ++y) {
|
||||
for (x = 0; x < width; ++x) {
|
||||
if (i > image_size) return;
|
||||
/* Extract the color */
|
||||
uint32_t color = bufferb[i + 3 * x] +
|
||||
bufferb[i+1 + 3 * x] * 0x100 +
|
||||
bufferb[i+2 + 3 * x] * 0x10000;
|
||||
uint32_t color;
|
||||
if (bpp == 24) {
|
||||
color = bufferb[i + 3 * x] +
|
||||
bufferb[i+1 + 3 * x] * 0x100 +
|
||||
bufferb[i+2 + 3 * x] * 0x10000;
|
||||
} else if (bpp == 32) {
|
||||
color = bufferb[i + 4 * x] * 0x1000000 +
|
||||
bufferb[i+1 + 4 * x] * 0x100 +
|
||||
bufferb[i+2 + 4 * x] * 0x10000 +
|
||||
bufferb[i+3 + 4 * x] * 0x1;
|
||||
}
|
||||
/* Set our point */
|
||||
sprite->bitmap[(height - y) * width + x] = color;
|
||||
}
|
||||
@ -103,11 +117,27 @@ load_sprite(sprite_t * sprite, char * filename) {
|
||||
free(bufferb);
|
||||
}
|
||||
|
||||
#define _RED(color) ((color & 0x00FF0000) / 0x10000)
|
||||
#define _GRE(color) ((color & 0x0000FF00) / 0x100)
|
||||
#define _BLU(color) ((color & 0x000000FF) / 0x1)
|
||||
|
||||
uint32_t alpha_blend(uint32_t bottom, uint32_t top, uint32_t mask) {
|
||||
float a = _RED(mask) / 256.0;
|
||||
uint8_t red = _RED(bottom) * (1.0 - a) + _RED(top) * a;
|
||||
uint8_t gre = _GRE(bottom) * (1.0 - a) + _GRE(top) * a;
|
||||
uint8_t blu = _BLU(bottom) * (1.0 - a) + _BLU(top) * a;
|
||||
return rgb(red,gre,blu);
|
||||
}
|
||||
|
||||
void draw_sprite(sprite_t * sprite, uint16_t x, uint16_t y) {
|
||||
for (uint16_t _y = 0; _y < sprite->height; ++_y) {
|
||||
for (uint16_t _x = 0; _x < sprite->width; ++_x) {
|
||||
if (SPRITE(sprite,_x,_y) != sprite->blank) {
|
||||
GFX(x + _x, y + _y) = SPRITE(sprite, _x, _y);
|
||||
if (sprite->alpha) {
|
||||
GFX(x + _x, y + _y) = alpha_blend(GFX(x + _x, y + _y), SPRITE(sprite, _x, _y), SMASKS(sprite, _x, _y));
|
||||
} else {
|
||||
if (SPRITE(sprite,_x,_y) != sprite->blank) {
|
||||
GFX(x + _x, y + _y) = SPRITE(sprite, _x, _y);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -141,38 +171,32 @@ void draw_line(uint16_t x0, uint16_t x1, uint16_t y0, uint16_t y1, uint32_t colo
|
||||
}
|
||||
}
|
||||
|
||||
/* woah */
|
||||
char font_buffer[400000];
|
||||
sprite_t alpha_tmp;
|
||||
|
||||
void init_sprite(int i, char * filename, char * alpha) {
|
||||
sprites[i] = malloc_(sizeof(sprite_t));
|
||||
load_sprite(sprites[i], filename);
|
||||
if (alpha) {
|
||||
sprites[i]->alpha = 1;
|
||||
load_sprite(&alpha_tmp, alpha);
|
||||
sprites[i]->masks = alpha_tmp.bitmap;
|
||||
} else {
|
||||
sprites[i]->alpha = 0;
|
||||
}
|
||||
sprites[i]->blank = 0x0;
|
||||
}
|
||||
|
||||
int main(int argc, char ** argv) {
|
||||
|
||||
gfx_mem = (void *)syscall_getgraphicsaddress();
|
||||
frame_mem = (void *)((uintptr_t)gfx_mem + sizeof(uint32_t) * 1024 * 768); //malloc(sizeof(uint32_t) * 1024 * 768);
|
||||
frame_mem = (void *)((uintptr_t)gfx_mem + sizeof(uint32_t) * 1024 * 768); //malloc_(sizeof(uint32_t) * 1024 * 768);
|
||||
printf("Graphics memory is at %p, backbuffer is at %p.\n", gfx_mem, frame_mem);
|
||||
|
||||
printf("Loading sprites...\n");
|
||||
sprites[0] = malloc(sizeof(sprite_t));
|
||||
load_sprite(sprites[0], "/bs.bmp");
|
||||
sprites[0]->blank = 0x0;
|
||||
printf("Sprite is %d by %d\n", sprites[0]->width, sprites[0]->height);
|
||||
printf("%x\n", sprites[0]->bitmap);
|
||||
|
||||
#if 1
|
||||
printf("Initialzing Freetype...\n");
|
||||
int error = FT_Init_FreeType(&library);
|
||||
if (error) {
|
||||
printf("FreeType initialization returned %d.\n");
|
||||
return error;
|
||||
}
|
||||
printf("Loading DejaVu Sans font.\n");
|
||||
error = FT_New_Face(library,
|
||||
"/etc/DejaVuSansMono.ttf",
|
||||
0,
|
||||
&face);
|
||||
if (error == FT_Err_Unknown_File_Format) {
|
||||
printf("FT: Unknown format (%d).\n", FT_Err_Unknown_File_Format);
|
||||
return error;
|
||||
} else {
|
||||
printf("FT: Something else went wrong. (%d)\n", error);
|
||||
}
|
||||
#endif
|
||||
|
||||
init_sprite(0, "/etc/ball.bmp", NULL);
|
||||
init_sprite(1, "/etc/toaru_logo.bmp", "/etc/toaru_logo_a.bmp");
|
||||
|
||||
printf("\033[J\n");
|
||||
|
||||
@ -180,22 +204,15 @@ int main(int argc, char ** argv) {
|
||||
|
||||
int playing = 1;
|
||||
|
||||
int obj_x = 0;
|
||||
int obj_y = 0;
|
||||
int obj_x = GFX_W / 2;
|
||||
int obj_y = GFX_H - 200;
|
||||
|
||||
int obj_h = 5;
|
||||
int obj_v = 5;
|
||||
int obj_h = 1;
|
||||
int obj_v = 1;
|
||||
|
||||
uint32_t which = 0;
|
||||
|
||||
while (playing) {
|
||||
#if 0
|
||||
uint32_t c = 0; //0x72A0CF; /* A nice sky blue */
|
||||
/* Clear the background */
|
||||
for (uint16_t x = 0; x < 1024; ++x) {
|
||||
for (uint16_t y = 0; y < 768; ++y) {
|
||||
GFX(x,y) = c;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Update the sprite location */
|
||||
obj_x += obj_h;
|
||||
@ -204,20 +221,20 @@ int main(int argc, char ** argv) {
|
||||
obj_x = 0;
|
||||
obj_h = -obj_h;
|
||||
}
|
||||
if (obj_x > GFX_W - sprites[0]->width) {
|
||||
obj_x = GFX_W - sprites[0]->width;
|
||||
if (obj_x > GFX_W - sprites[which]->width) {
|
||||
obj_x = GFX_W - sprites[which]->width;
|
||||
obj_h = -obj_h;
|
||||
}
|
||||
if (obj_y < 0) {
|
||||
obj_y = 0;
|
||||
obj_v = -obj_v;
|
||||
}
|
||||
if (obj_y > GFX_H - sprites[0]->height) {
|
||||
obj_y = GFX_H - sprites[0]->height;
|
||||
if (obj_y > GFX_H - sprites[which]->height) {
|
||||
obj_y = GFX_H - sprites[which]->height;
|
||||
obj_v = -obj_v;
|
||||
}
|
||||
|
||||
draw_sprite(sprites[0], obj_x, obj_y);
|
||||
draw_sprite(sprites[which], obj_x, obj_y);
|
||||
flip();
|
||||
|
||||
char ch = 0;
|
||||
@ -226,26 +243,27 @@ int main(int argc, char ** argv) {
|
||||
case 16:
|
||||
playing = 0;
|
||||
break;
|
||||
case 17:
|
||||
--obj_v;
|
||||
break;
|
||||
case 31:
|
||||
++obj_v;
|
||||
break;
|
||||
case 30:
|
||||
--obj_h;
|
||||
obj_h = -1;
|
||||
/* left */
|
||||
break;
|
||||
case 32:
|
||||
++obj_h;
|
||||
obj_h = 1;
|
||||
/* right */
|
||||
break;
|
||||
case 18:
|
||||
obj_v = 0;
|
||||
obj_h = 0;
|
||||
obj_x = GFX_W / 2;
|
||||
obj_y = GFX_H - 200;
|
||||
obj_v = 1;
|
||||
obj_h = 1;
|
||||
break;
|
||||
case 2:
|
||||
which = 0;
|
||||
break;
|
||||
case 3:
|
||||
which = 1;
|
||||
break;
|
||||
default:
|
||||
if (ch) {
|
||||
printf("%d\n", ch);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
Binary file not shown.
BIN
initrd/etc/ball.bmp
Normal file
BIN
initrd/etc/ball.bmp
Normal file
Binary file not shown.
After Width: | Height: | Size: 246 B |
BIN
initrd/etc/toaru_logo.bmp
Normal file
BIN
initrd/etc/toaru_logo.bmp
Normal file
Binary file not shown.
After Width: | Height: | Size: 64 KiB |
BIN
initrd/etc/toaru_logo_a.bmp
Normal file
BIN
initrd/etc/toaru_logo_a.bmp
Normal file
Binary file not shown.
After Width: | Height: | Size: 48 KiB |
Loading…
Reference in New Issue
Block a user