-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ Implemented funtion for reading and printing txt input. + Allegro not working yet.
- Loading branch information
Marcelo Politzer Couto
committed
Mar 30, 2012
0 parents
commit a30191e
Showing
6 changed files
with
252 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,22 @@ | ||
CC=gcc | ||
CFLAGS=-O2 -funroll-loops -c $(pkg-config allegro-5.0 --cflags) | ||
LDFLAGS=-O2 | ||
LDLIBS=-lm `pkg-config allegro-5.0 --libs` | ||
SOURCES=main.c game.c | ||
HEADERS=game.h | ||
OBJECTS=$(addsuffix .o, $(basename ${SOURCES})) | ||
EXECUTABLE=main | ||
|
||
all: $(EXECUTABLE) | ||
|
||
zip: Makefile $(SOURCES) $(HEADERS) | ||
zip -r mypackage.zip $^ | ||
|
||
$(EXECUTABLE): $(OBJECTS) | ||
|
||
$(OBJECTS): %.o: %.c $(HEADERS) | ||
|
||
clean: | ||
rm -f $(EXECUTABLE) $(OBJECTS) | ||
|
||
.PHONY: all clean |
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,52 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <stdarg.h> | ||
|
||
#include "game.h" | ||
|
||
#define die(error, ...) _die(error, __FILE__, __LINE__, __VA_ARGS__) | ||
|
||
void _die(int error, char* filename, int line, const char* format, ...) | ||
{ | ||
va_list args; | ||
|
||
fprintf(stderr, "[%s:%d] ", filename, line); | ||
va_start(args, format); | ||
vfprintf(stderr, format, args); | ||
va_end(args); | ||
fprintf(stderr, "\n"); | ||
exit(error); | ||
} | ||
|
||
void game_init(int w, int h, int fps) | ||
{ | ||
if (!al_init()) | ||
die(1, "can't init allegro\n"); | ||
if (!(G.display = al_create_display(w, h))) | ||
die(2, "can't create display"); | ||
if (!(G.tick = al_create_timer(1.0 / fps))) | ||
die(3, "invalid fps"); | ||
} | ||
|
||
void game_loop(void) | ||
{ | ||
int done = 0; | ||
while (!done){ | ||
ALLEGRO_EVENT ev; | ||
al_wait_for_event(G.ev, &ev); | ||
game_render(); | ||
} | ||
} | ||
|
||
void game_render(void) | ||
{ | ||
al_clear_to_color(al_map_rgb(0,0,0)); | ||
al_flip_display(); | ||
} | ||
|
||
void game_end(void) | ||
{ | ||
al_destroy_display(G.display); | ||
al_destroy_timer(G.tick); | ||
al_destroy_event_queue(G.ev); | ||
} |
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,41 @@ | ||
#ifndef GAME_H | ||
#define GAME_H | ||
|
||
#include <allegro5/allegro.h> | ||
|
||
struct pos { | ||
unsigned int x, y; | ||
}; | ||
|
||
struct game_state { | ||
struct pos *bonus_pos; | ||
int bonus_count; | ||
}; | ||
|
||
struct game_info { | ||
int start[2]; | ||
int end[2]; | ||
int num_prizes; | ||
int (*prizes)[2]; | ||
int map_size[2]; | ||
int *mapv; /* map of values */ | ||
char *map; /* map of characters */ | ||
}; | ||
|
||
struct game { | ||
ALLEGRO_DISPLAY *display; | ||
|
||
ALLEGRO_TIMER *tick; | ||
ALLEGRO_EVENT_QUEUE *ev; | ||
struct game_info gi; | ||
struct game_state gs; | ||
}; | ||
|
||
extern struct game G; | ||
|
||
void game_init(int w, int h, int fps); | ||
void game_render(void); | ||
void game_step(void); | ||
void game_end(void); | ||
|
||
#endif /* GAME_H */ |
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,34 @@ | ||
1 1 | ||
10 10 | ||
10 | ||
2 2 | ||
4 4 | ||
11 11 | ||
9 9 | ||
18 17 | ||
10 5 | ||
5 8 | ||
7 7 | ||
1 10 | ||
2 19 | ||
20 20 | ||
XXXXXXXXXXXXXXXXXXXX | ||
XRXAAAAPSSAPPSSXXRRX | ||
XRXAAAAXSSAPPSSSSRRX | ||
XRXXXAAXSSXXXSSXSXSX | ||
XRRRXAAXXAXSASSXSXSX | ||
XRRRXSSXSSSSXXXXSRRX | ||
XPXRXXSXSSSSXSSXAXAX | ||
XSSSSSSSSXXXXSSXAXSX | ||
XSSSXXXSARRRPPSXAXAX | ||
XSSSXSPSXXXAXXRXXXSX | ||
XSSSRSXSRSASSXRRRRRX | ||
XSSSXSRSRPASSPSSSSSX | ||
XXPXXSXXXXXSRXSXXXXX | ||
XSSRRRXSARXSRXSXSSSX | ||
XSSAASRSXRXRRXPXSASX | ||
XXSXPXXSXRXSXXPXSXSX | ||
XRSPPSSSXXXSXPPXSXSX | ||
XRRPAXXSSSSSXXXXSXSX | ||
XSSXSSSSXXAASSSSSPSX | ||
XXXXXXXXXXXXXXXXXXXX |
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,91 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <assert.h> | ||
|
||
#include "game.h" | ||
|
||
struct game G; | ||
|
||
int read_map(void) | ||
{ | ||
int i, j, unused; | ||
assert(scanf(" %d %d", &G.gi.start[0], &G.gi.start[1]) == 2); | ||
assert(scanf(" %d %d", &G.gi.end[0], &G.gi.end[1]) == 2); | ||
assert(scanf(" %d", &G.gi.num_prizes) == 1); | ||
G.gi.prizes = malloc(sizeof(int) * 2 * G.gi.num_prizes); | ||
for (i=0; i<G.gi.num_prizes; i++) { | ||
int x, y; | ||
assert(scanf(" %d %d", &x, &y) == 2); | ||
G.gi.prizes[i][0] = x; | ||
G.gi.prizes[i][1] = y; | ||
} | ||
assert(scanf(" %d %d", &G.gi.map_size[0], &G.gi.map_size[1]) == 2); | ||
G.gi.map = malloc(sizeof(char) * G.gi.map_size[0] * G.gi.map_size[1]); | ||
G.gi.mapv = malloc(sizeof(int) * G.gi.map_size[0] * G.gi.map_size[1]); | ||
/* read tile values */ | ||
for (i=0; i<G.gi.map_size[0]; i++) { | ||
for (j = 0; j < G.gi.map_size[1]; j++) { | ||
int map_index = i + j * G.gi.map_size[1]; | ||
char c; | ||
|
||
do { | ||
c = getchar(); | ||
} while (!isalnum(c)); | ||
|
||
c = tolower(c); | ||
|
||
G.gi.map[map_index] = c; | ||
switch (c){ | ||
case 'x': | ||
G.gi.mapv[map_index] = -1; | ||
break; | ||
case 's': | ||
G.gi.mapv[map_index] = 1; | ||
break; | ||
case 'a': | ||
G.gi.mapv[map_index] = 4; | ||
break; | ||
case 'r': | ||
G.gi.mapv[map_index] = 10; | ||
break; | ||
case 'p': | ||
G.gi.mapv[map_index] = 20; | ||
break; | ||
} | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
void print_map(void) | ||
{ | ||
int i, j; | ||
printf("start (%2d,%2d)\n", G.gi.start[0], G.gi.start[1]); | ||
printf("end (%2d,%2d)\n", G.gi.end[0], G.gi.end[1]); | ||
printf("num prizes %2d\n", G.gi.num_prizes); | ||
for (i=0; i<G.gi.num_prizes; i++) | ||
printf("\t(%2d,%2d)\n", i, G.gi.prizes[i][0], G.gi.prizes[i][1]); | ||
printf("map_size (%2d,%2d)\n", G.gi.map_size[0], G.gi.map_size[1]); | ||
for (i=0; i<G.gi.map_size[0]; i++){ | ||
for (j=0; j<G.gi.map_size[1]; j++){ | ||
int map_index = i + j * G.gi.map_size[1]; | ||
printf("%c", G.gi.map[map_index]); | ||
} | ||
printf(" |"); | ||
for (j=0; j<G.gi.map_size[1]; j++){ | ||
int map_index = i + j * G.gi.map_size[1]; | ||
printf("%3d", G.gi.mapv[map_index]); | ||
} | ||
printf("\n"); | ||
} | ||
} | ||
|
||
int main(int argc, const char *argv[]) | ||
{ | ||
read_map(); | ||
print_map(); | ||
|
||
game_init(800, 600, 60); | ||
game_end(); | ||
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,12 @@ | ||
start_x start_y | ||
end_x end_y | ||
num_prizes | ||
prizes | ||
x y ... | ||
map_w map_h | ||
# X - wall | ||
# S - 1 | ||
# A - 4 | ||
# R - 10 | ||
# P - 20 | ||
line ... |