commit
80a6e4948a
9 changed files with 278 additions and 0 deletions
@ -0,0 +1,21 @@
|
||||
The MIT License (MIT) |
||||
|
||||
Copyright (c) 2019 katherine |
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy |
||||
of this software and associated documentation files (the "Software"), to deal |
||||
in the Software without restriction, including without limitation the rights |
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||||
copies of the Software, and to permit persons to whom the Software is |
||||
furnished to do so, subject to the following conditions: |
||||
|
||||
The above copyright notice and this permission notice shall be included in |
||||
all copies or substantial portions of the Software. |
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
||||
THE SOFTWARE. |
@ -0,0 +1,29 @@
|
||||
CFLAGS+=-Wall -O2 $(shell pkg-config --cflags allegro-5 allegro_image-5 allegro_font-5 allegro_ttf-5)
|
||||
CFLAGSDEBUG=-Wall -ggdb3 -O0 -DDEBUG $(shell pkg-config --cflags allegro-5 allegro_image-5 allegro_font-5 allegro_ttf-5)
|
||||
LDFLAGS+=-Wall -O2 $(shell pkg-config --libs allegro-5 allegro_image-5 allegro_font-5 allegro_ttf-5)
|
||||
LDFLAGSDEBUG=-Wall -ggdb3 -O0 -DDEBUG $(shell pkg-config --libs allegro-5 allegro_image-5 allegro_font-5 allegro_ttf-5)
|
||||
SRCDIR=./src
|
||||
OBJDIR=./obj
|
||||
SRC=$(wildcard $(SRCDIR)/*.c)
|
||||
OBJ=$(patsubst $(SRCDIR)%.c,$(OBJDIR)%.o,$(SRC))
|
||||
BIN=gafu
|
||||
|
||||
all: $(OBJ) |
||||
$(CC) $(LDFLAGS) -o $(BIN) $^
|
||||
|
||||
$(OBJ): | $(OBJDIR) |
||||
|
||||
$(OBJDIR)/%.o: $(SRCDIR)/%.c |
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
$(OBJDIR): |
||||
mkdir -p $(OBJDIR)
|
||||
|
||||
clean: |
||||
rm -rf $(OBJDIR) $(BIN)
|
||||
|
||||
debug: CFLAGS=$(CFLAGSDEBUG) |
||||
debug: LDFLAGS=$(LDFLAGSDEBUG) |
||||
debug: all |
||||
|
||||
new: clean all |
@ -0,0 +1,3 @@
|
||||
beginnings of an allegro5-based game engine |
||||
|
||||
nothing special here just yet. check back later |
@ -0,0 +1,21 @@
|
||||
#ifndef GAFU_ERR_H |
||||
#define GAFU_ERR_H |
||||
|
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
|
||||
#define ERR(...) \ |
||||
do { \
|
||||
fprintf(stderr, "error: " __VA_ARGS__); \
|
||||
fprintf(stderr, "\n"); \
|
||||
exit(EXIT_FAILURE); \
|
||||
} while (0) |
||||
|
||||
#define TRY(cond, ...) \ |
||||
do { \
|
||||
if (!(cond)) { \
|
||||
ERR(__VA_ARGS__); \
|
||||
} \
|
||||
} while (0) |
||||
|
||||
#endif |
@ -0,0 +1,107 @@
|
||||
#include "err.h" |
||||
#include "set.h" |
||||
|
||||
#include <stdbool.h> |
||||
|
||||
#include <allegro5/allegro5.h> |
||||
#include <allegro5/allegro_font.h> |
||||
#include <allegro5/allegro_ttf.h> |
||||
#include <allegro5/allegro_image.h> |
||||
|
||||
#define SCREEN_WIDTH 480 |
||||
#define SCREEN_HEIGHT 360 |
||||
static float screen_scale = 2; |
||||
|
||||
static inline void init_resources(ALLEGRO_DISPLAY **d, ALLEGRO_FONT **f, |
||||
ALLEGRO_EVENT_QUEUE **q, ALLEGRO_TIMER **t) |
||||
{ |
||||
TRY(al_init(), "failed to initialise allegro"); |
||||
TRY(al_install_keyboard(), "failed to initialise allegro keyboard"); |
||||
TRY(al_init_font_addon(), "failed to initialise allegro font addon"); |
||||
TRY(al_init_ttf_addon(), "failed to initialise allegro ttf addon"); |
||||
TRY(al_init_image_addon(), "failed to initialise allegro image addon"); |
||||
|
||||
al_set_new_display_flags(ALLEGRO_WINDOWED); |
||||
al_set_new_window_title("ただのテスト"); |
||||
*d = al_create_display(SCREEN_WIDTH * screen_scale, SCREEN_HEIGHT * screen_scale); |
||||
TRY(*d != NULL, "failed to create display"); |
||||
|
||||
*f = al_load_font("data/font/efont/BiwidthMedium.ttf", -24, ALLEGRO_TTF_MONOCHROME | ALLEGRO_TTF_NO_AUTOHINT); |
||||
TRY(*f != NULL, "failed to load font"); |
||||
|
||||
*q = al_create_event_queue(); |
||||
TRY(*q != NULL, "failed to create event queue"); |
||||
|
||||
*t = al_create_timer(1.0 / 30.0); |
||||
TRY(*t != NULL, "failed to create timer"); |
||||
|
||||
al_register_event_source(*q, al_get_keyboard_event_source()); |
||||
al_register_event_source(*q, al_get_display_event_source(*d)); |
||||
al_register_event_source(*q, al_get_timer_event_source(*t)); |
||||
} |
||||
|
||||
int main(int argc, char **argv) |
||||
{ |
||||
ALLEGRO_TIMER *t; |
||||
ALLEGRO_EVENT_QUEUE *q; |
||||
ALLEGRO_DISPLAY *d; |
||||
ALLEGRO_FONT *f; |
||||
ALLEGRO_EVENT e; |
||||
ALLEGRO_BITMAP *buf; |
||||
struct set *s; |
||||
|
||||
init_resources(&d, &f, &q, &t); |
||||
|
||||
buf = al_create_bitmap(SCREEN_WIDTH, SCREEN_HEIGHT); |
||||
|
||||
s = set_load("set", 32, 32, 2); |
||||
|
||||
al_start_timer(t); |
||||
|
||||
while (true) { |
||||
al_wait_for_event(q, &e); |
||||
|
||||
switch (e.type) { |
||||
case ALLEGRO_EVENT_TIMER: |
||||
break; |
||||
|
||||
case ALLEGRO_EVENT_KEY_DOWN: |
||||
if (e.keyboard.keycode != ALLEGRO_KEY_Q) |
||||
continue; |
||||
case ALLEGRO_EVENT_DISPLAY_CLOSE: |
||||
goto done; |
||||
|
||||
default: |
||||
continue; |
||||
} |
||||
|
||||
|
||||
al_set_target_bitmap(buf); |
||||
|
||||
al_clear_to_color(al_map_rgb(0, 0, 0)); |
||||
al_draw_bitmap(set_at(s, 0)->b, 0, 0, 0); |
||||
al_draw_text(f, al_map_rgb(255, 255, 255), |
||||
240, 320, |
||||
ALLEGRO_ALIGN_CENTRE | ALLEGRO_ALIGN_INTEGER, |
||||
"マイクチェック"); |
||||
|
||||
al_set_target_backbuffer(d); |
||||
|
||||
al_draw_scaled_bitmap(buf, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, |
||||
0, 0, |
||||
screen_scale * SCREEN_WIDTH, screen_scale * SCREEN_HEIGHT, |
||||
0); |
||||
|
||||
al_flip_display(); |
||||
} |
||||
|
||||
done: |
||||
al_destroy_bitmap(buf); |
||||
al_destroy_font(f); |
||||
al_destroy_display(d); |
||||
al_destroy_timer(t); |
||||
al_destroy_event_queue(q); |
||||
al_uninstall_system(); |
||||
|
||||
return EXIT_SUCCESS; |
||||
} |
@ -0,0 +1,19 @@
|
||||
#ifndef GAFU_MAP_H |
||||
#define GAFU_MAP_H |
||||
|
||||
#include "set.h" |
||||
|
||||
struct map_plane { |
||||
unsigned width; |
||||
unsigned height; |
||||
unsigned *plane; |
||||
} |
||||
|
||||
struct map { |
||||
unsigned set_count; |
||||
struct set **sets; |
||||
unsigned plane_count; |
||||
struct map_plane *planes; |
||||
} |
||||
|
||||
#endif |
@ -0,0 +1,56 @@
|
||||
#include "set.h" |
||||
#include "err.h" |
||||
|
||||
#include <assert.h> |
||||
|
||||
struct set* set_load(const char *fname, unsigned tw, unsigned th, unsigned bw) |
||||
{ |
||||
unsigned w, h; |
||||
unsigned i; |
||||
unsigned x, y; |
||||
struct set *s; |
||||
char buf[1024] = "data/tileset/"; |
||||
|
||||
assert(tw); |
||||
assert(th); |
||||
assert(fname); |
||||
|
||||
TRY(strlen(fname) - 1 < 1006, "bad tileset name"); |
||||
strcpy(buf + 13, fname); |
||||
strcpy(buf + 13 + strlen(fname), ".png"); |
||||
|
||||
s = malloc(sizeof(*s)); |
||||
assert(s != NULL); |
||||
|
||||
s->body = al_load_bitmap(buf); |
||||
TRY(s->body != NULL, "could not load tileset `%s`", fname); |
||||
|
||||
w = (unsigned)al_get_bitmap_width(s->body); |
||||
h = (unsigned)al_get_bitmap_height(s->body); |
||||
|
||||
TRY(w % (tw + bw) == 0, "bad tile width for tileset `%s`", fname); |
||||
TRY(h % (th + bw) == 0, "bad tile width for tileset `%s`", fname); |
||||
|
||||
s->tile_count = (w / (tw + bw)) * (h / (th + bw)); |
||||
|
||||
s->tiles = malloc(s->tile_count * sizeof(*(s->tiles))); |
||||
assert(s->tiles != NULL); |
||||
|
||||
i = 0; |
||||
for (y = 0; y < h; y += (th + bw)) { |
||||
for (x = 0; x < w; x += (tw + bw)) { |
||||
s->tiles[i].b = al_create_sub_bitmap(s->body, x, y, tw, th); |
||||
i++; |
||||
} |
||||
} |
||||
|
||||
return s; |
||||
} |
||||
|
||||
struct set_tile* set_at(struct set *s, unsigned n) |
||||
{ |
||||
assert(s != NULL); |
||||
assert(n < s->tile_count); |
||||
|
||||
return &(s->tiles[n]); |
||||
} |
@ -0,0 +1,22 @@
|
||||
#ifndef GAFU_SET_H |
||||
#define GAFU_SET_H |
||||
|
||||
#include <allegro5/allegro.h> |
||||
#include <allegro5/allegro_image.h> |
||||
|
||||
struct set_tile { |
||||
bool walkable; |
||||
ALLEGRO_BITMAP *b; |
||||
}; |
||||
|
||||
struct set { |
||||
ALLEGRO_BITMAP *body; |
||||
unsigned tile_count; |
||||
struct set_tile *tiles; |
||||
}; |
||||
|
||||
struct set* set_load(const char *fname, unsigned tw, unsigned th, unsigned bw); |
||||
|
||||
struct set_tile* set_at(struct set *s, unsigned n); |
||||
|
||||
#endif |
Loading…
Reference in new issue