Index: apps/snake/SOURCES |
— | — | @@ -0,0 +1 @@ |
| 2 | +main.c
|
Index: apps/snake/ls.x |
— | — | @@ -0,0 +1,42 @@ |
| 2 | +ENTRY(__emcore_entrypoint)
|
| 3 | +OUTPUT_FORMAT(elf32-littlearm)
|
| 4 | +OUTPUT_ARCH(arm)
|
| 5 | +
|
| 6 | +MEMORY
|
| 7 | +{
|
| 8 | + VIRTUAL : ORIGIN = 0x00000000, LENGTH = 0x10000000
|
| 9 | +}
|
| 10 | +
|
| 11 | +SECTIONS
|
| 12 | +{
|
| 13 | + .text :
|
| 14 | + {
|
| 15 | + __emcore_app_base = .;
|
| 16 | + KEEP(.emcoreentrypoint*)
|
| 17 | + *(.emcoreentrypoint*)
|
| 18 | + *(.text*)
|
| 19 | + *(.glue_7)
|
| 20 | + *(.glue_7t)
|
| 21 | + . = ALIGN(0x10);
|
| 22 | + } > VIRTUAL
|
| 23 | +
|
| 24 | + .data :
|
| 25 | + {
|
| 26 | + *(.rodata*)
|
| 27 | + . = ALIGN(0x4);
|
| 28 | + *(.data*)
|
| 29 | + . = ALIGN(0x10);
|
| 30 | + } > VIRTUAL
|
| 31 | +
|
| 32 | + .bss (NOLOAD) :
|
| 33 | + {
|
| 34 | + *(.bss*)
|
| 35 | + *(COMMON)
|
| 36 | + } > VIRTUAL
|
| 37 | +
|
| 38 | + /DISCARD/ :
|
| 39 | + {
|
| 40 | + *(.eh_frame)
|
| 41 | + }
|
| 42 | +
|
| 43 | +}
|
Index: apps/snake/main.c |
— | — | @@ -0,0 +1,215 @@ |
| 2 | +#include "emcoreapp.h"
|
| 3 | +#include "libui.h"
|
| 4 | +
|
| 5 | +
|
| 6 | +#define TILE_W 5
|
| 7 | +#define TILE_H 5
|
| 8 | +
|
| 9 | +
|
| 10 | +enum state
|
| 11 | +{
|
| 12 | + STATE_FREE,
|
| 13 | + STATE_SNAKE,
|
| 14 | + STATE_FOOD,
|
| 15 | + STATE_OUTOFBOUNDS,
|
| 16 | + NUM_STATES
|
| 17 | +};
|
| 18 | +
|
| 19 | +struct pos
|
| 20 | +{
|
| 21 | + uint8_t x;
|
| 22 | + uint8_t y;
|
| 23 | +};
|
| 24 | +
|
| 25 | +
|
| 26 | +uint32_t colors[NUM_STATES] =
|
| 27 | +{
|
| 28 | + [STATE_FREE] = 0,
|
| 29 | + [STATE_SNAKE] = 0xff000000,
|
| 30 | + [STATE_FOOD] = 0xffff0000
|
| 31 | +};
|
| 32 | +
|
| 33 | +
|
| 34 | +struct libui_api* ui;
|
| 35 | +int dirx;
|
| 36 | +int diry;
|
| 37 | +int offsetx;
|
| 38 | +int offsety;
|
| 39 | +int tilesx;
|
| 40 | +int tilesy;
|
| 41 | +int tilecount;
|
| 42 | +void* framebuf;
|
| 43 | +int framebufx;
|
| 44 | +int framebufy;
|
| 45 | +int framebufsize;
|
| 46 | +uint8_t* field;
|
| 47 | +struct pos* snake;
|
| 48 | +int snakehead;
|
| 49 | +int snaketail;
|
| 50 | +int snakelength;
|
| 51 | +int xrandshift;
|
| 52 | +int yrandshift;
|
| 53 | +
|
| 54 | +
|
| 55 | +void buttonhandler(void* user, enum button_event eventtype, int which, int value)
|
| 56 | +{
|
| 57 | + if (eventtype == BUTTON_PRESS)
|
| 58 | + switch (which)
|
| 59 | + {
|
| 60 | + case 1:
|
| 61 | + dirx = 1;
|
| 62 | + diry = 0;
|
| 63 | + break;
|
| 64 | + case 2:
|
| 65 | + dirx = -1;
|
| 66 | + diry = 0;
|
| 67 | + break;
|
| 68 | + case 3:
|
| 69 | + dirx = 0;
|
| 70 | + diry = 1;
|
| 71 | + break;
|
| 72 | + case 4:
|
| 73 | + dirx = 0;
|
| 74 | + diry = -1;
|
| 75 | + break;
|
| 76 | + }
|
| 77 | +}
|
| 78 | +
|
| 79 | +void setstate(int x, int y, enum state s)
|
| 80 | +{
|
| 81 | + if (x < 0 || x >= tilesx || y < 0 || y >= tilesy) return;
|
| 82 | + field[y * tilesx + x] = (uint8_t)s;
|
| 83 | +}
|
| 84 | +
|
| 85 | +enum state getstate(int x, int y)
|
| 86 | +{
|
| 87 | + if (x < 0 || x >= tilesx || y < 0 || y >= tilesy) return STATE_OUTOFBOUNDS;
|
| 88 | + return (enum state)field[y * tilesx + x];
|
| 89 | +}
|
| 90 | +
|
| 91 | +enum state extendsnake()
|
| 92 | +{
|
| 93 | + int newx = snake[snakehead].x + dirx;
|
| 94 | + int newy = snake[snakehead].y + diry;
|
| 95 | + enum state s = getstate(newx, newy);
|
| 96 | + if (s != STATE_FREE && s != STATE_FOOD) return s;
|
| 97 | + snakehead++;
|
| 98 | + setstate(newx, newy, STATE_SNAKE);
|
| 99 | + if (snakehead >= tilecount) snakehead = 0;
|
| 100 | + snake[snakehead].x = newx;
|
| 101 | + snake[snakehead].y = newy;
|
| 102 | + snakelength++;
|
| 103 | + return s;
|
| 104 | +}
|
| 105 | +
|
| 106 | +void shrinksnake()
|
| 107 | +{
|
| 108 | + setstate(snake[snaketail].x, snake[snaketail].y, STATE_FREE);
|
| 109 | + snaketail++;
|
| 110 | + if (snaketail >= tilecount) snaketail = 0;
|
| 111 | + snakelength--;
|
| 112 | +}
|
| 113 | +
|
| 114 | +void placefood()
|
| 115 | +{
|
| 116 | + int x;
|
| 117 | + int y;
|
| 118 | + enum state s = STATE_OUTOFBOUNDS;
|
| 119 | + while (s != STATE_FREE)
|
| 120 | + {
|
| 121 | + x = rand() >> xrandshift;
|
| 122 | + y = rand() >> yrandshift;
|
| 123 | + s = getstate(x, y);
|
| 124 | + }
|
| 125 | + setstate(x, y, STATE_FOOD);
|
| 126 | +}
|
| 127 | +
|
| 128 | +void draw()
|
| 129 | +{
|
| 130 | + int x, y;
|
| 131 | + memset(framebuf, 0xff, framebufsize);
|
| 132 | + for (y = 0; y < tilesy; y++)
|
| 133 | + for (x = 0; x < tilesx; x++)
|
| 134 | + {
|
| 135 | + uint32_t color = colors[getstate(x, y)];
|
| 136 | + if (color) ui->fill(TILE_W, TILE_H, color, framebuf, x * TILE_W, y * TILE_H, framebufx);
|
| 137 | + }
|
| 138 | + displaylcd(offsetx, offsety, framebufx, framebufy, framebuf, 0, 0, framebufx);
|
| 139 | +}
|
| 140 | +
|
| 141 | +
|
| 142 | +static void main()
|
| 143 | +{
|
| 144 | + int i;
|
| 145 | +
|
| 146 | + struct emcorelib_header* libui = get_library(0x49554365, LIBUI_API_VERSION, LIBSOURCE_BOOTFLASH, "libui ");
|
| 147 | + if (!libui) panicf(PANIC_KILLTHREAD, "Could not load user interface library!");
|
| 148 | + ui = (struct libui_api*)libui->api;
|
| 149 | +
|
| 150 | + int width = lcd_get_width();
|
| 151 | + int height = lcd_get_height();
|
| 152 | + tilesx = width / TILE_W;
|
| 153 | + tilesy = height / TILE_H;
|
| 154 | + tilecount = tilesx * tilesy;
|
| 155 | + framebufx = tilesx * TILE_W;
|
| 156 | + framebufy = tilesy * TILE_H;
|
| 157 | + framebufsize = framebufx * framebufy * 3;
|
| 158 | + offsetx = (width - framebufx) / 2;
|
| 159 | + offsety = (height - framebufy) / 2;
|
| 160 | + xrandshift = __emcore_syscall->__clzsi2(tilesx - 1) - 1;
|
| 161 | + yrandshift = __emcore_syscall->__clzsi2(tilesy - 1) - 1;
|
| 162 | + framebuf = malloc(framebufsize);
|
| 163 | + field = malloc(tilecount * sizeof(typeof(field)));
|
| 164 | + snake = malloc(tilecount * sizeof(typeof(snake)));
|
| 165 | + if (!framebuf || !field || !snake) panicf(PANIC_KILLTHREAD, "Out of memory!");
|
| 166 | + struct button_hook_entry* hook = button_register_handler(buttonhandler, NULL);
|
| 167 | + if (!hook) panicf(PANIC_KILLTHREAD, "Could not register button hook!");
|
| 168 | +
|
| 169 | + memset(field, STATE_FREE, tilesx * tilesy);
|
| 170 | + bool finished = false;
|
| 171 | + dirx = 1;
|
| 172 | + diry = 0;
|
| 173 | + snakehead = 0;
|
| 174 | + snaketail = 0;
|
| 175 | + snakelength = 0;
|
| 176 | + snake[0].x = 10;
|
| 177 | + snake[0].y = 10;
|
| 178 | + for (i = 0; i < 10; i++) placefood();
|
| 179 | + long steptime = 500000;
|
| 180 | + long lasttime = USEC_TIMER - steptime;
|
| 181 | +
|
| 182 | + while (!finished)
|
| 183 | + {
|
| 184 | + long time = USEC_TIMER;
|
| 185 | + lasttime += steptime;
|
| 186 | + if (time >= lasttime) lasttime = time;
|
| 187 | + else sleep(lasttime - time);
|
| 188 | + bool food = false;
|
| 189 | + switch (extendsnake())
|
| 190 | + {
|
| 191 | + case STATE_SNAKE:
|
| 192 | + case STATE_OUTOFBOUNDS:
|
| 193 | + finished = true;
|
| 194 | + break;
|
| 195 | + case STATE_FOOD:
|
| 196 | + food = true;
|
| 197 | + }
|
| 198 | + if (food) placefood();
|
| 199 | + else if (snakelength > 5) shrinksnake();
|
| 200 | + if (steptime > 50000) steptime -= 100;
|
| 201 | + draw();
|
| 202 | + }
|
| 203 | +
|
| 204 | + cprintf(1, "Score: %d\n", snakelength);
|
| 205 | +
|
| 206 | + button_unregister_handler(hook);
|
| 207 | + free(snake);
|
| 208 | + free(field);
|
| 209 | + free(framebuf);
|
| 210 | +
|
| 211 | + release_library(libui);
|
| 212 | + library_unload(libui);
|
| 213 | +}
|
| 214 | +
|
| 215 | +
|
| 216 | +EMCORE_APP_HEADER("Snake", main, 127)
|
Index: apps/snake/version.h |
— | — | @@ -0,0 +1,36 @@ |
| 2 | +//
|
| 3 | +//
|
| 4 | +// Copyright 2010 TheSeven
|
| 5 | +//
|
| 6 | +//
|
| 7 | +// This file is part of emBIOS.
|
| 8 | +//
|
| 9 | +// emBIOS is free software: you can redistribute it and/or
|
| 10 | +// modify it under the terms of the GNU General Public License as
|
| 11 | +// published by the Free Software Foundation, either version 2 of the
|
| 12 | +// License, or (at your option) any later version.
|
| 13 | +//
|
| 14 | +// emBIOS is distributed in the hope that it will be useful,
|
| 15 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 16 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
| 17 | +// See the GNU General Public License for more details.
|
| 18 | +//
|
| 19 | +// You should have received a copy of the GNU General Public License along
|
| 20 | +// with emBIOS. If not, see <http://www.gnu.org/licenses/>.
|
| 21 | +//
|
| 22 | +//
|
| 23 | +
|
| 24 | +
|
| 25 | +#ifndef __VERSION_H__
|
| 26 | +#define __VERSION_H__
|
| 27 | +
|
| 28 | +
|
| 29 | +#define VERSION "0.0.1pre"
|
| 30 | +#define VERSION_MAJOR 0
|
| 31 | +#define VERSION_MINOR 0
|
| 32 | +#define VERSION_PATCH 1
|
| 33 | +#define VERSION_SVN "$REVISION$"
|
| 34 | +#define VERSION_SVN_INT $REVISIONINT$
|
| 35 | +
|
| 36 | +
|
| 37 | +#endif |
\ No newline at end of file |
Index: apps/snake/Makefile |
— | — | @@ -0,0 +1,121 @@ |
| 2 | +NAME := snake
|
| 3 | +STACKSIZE := 4096
|
| 4 | +COMPRESS := true
|
| 5 | +
|
| 6 | +EMCOREDIR ?= ../../emcore/trunk/
|
| 7 | +LIBUIDIR ?= ../../libs/ui/
|
| 8 | +
|
| 9 | +ifeq ($(shell uname),WindowsNT)
|
| 10 | +CCACHE :=
|
| 11 | +else
|
| 12 | +CCACHE := $(shell which ccache)
|
| 13 | +endif
|
| 14 | +
|
| 15 | +CROSS ?= arm-elf-eabi-
|
| 16 | +CC := $(CCACHE) $(CROSS)gcc
|
| 17 | +AS := $(CROSS)as
|
| 18 | +LD := $(CROSS)ld
|
| 19 | +OBJCOPY := $(CROSS)objcopy
|
| 20 | +ELF2ECA := $(CROSS)elf2emcoreapp
|
| 21 | +
|
| 22 | +LIBINCLUDES := -I$(LIBUIDIR)/export
|
| 23 | +
|
| 24 | +CFLAGS += -Os -fno-pie -fno-stack-protector -fomit-frame-pointer -I. -I$(EMCOREDIR)/export $(LIBINCLUDES) -ffunction-sections -fdata-sections -mcpu=arm940t -DARM_ARCH=4
|
| 25 | +LDFLAGS += "$(shell $(CC) -print-libgcc-file-name)" --emit-relocs --gc-sections
|
| 26 | +
|
| 27 | +preprocess = $(shell $(CC) $(PPCFLAGS) $(2) -E -P -x c $(1) | grep -v "^\#")
|
| 28 | +preprocesspaths = $(shell $(CC) $(PPCFLAGS) $(2) -E -P -x c $(1) | grep -v "^\#" | sed -e "s:^..*:$(dir $(1))&:")
|
| 29 | +
|
| 30 | +REVISION := $(shell svnversion .)
|
| 31 | +REVISIONINT := $(shell echo $(REVISION) | sed -e "s/[^0-9].*$$//")
|
| 32 | +
|
| 33 | +HELPERS := build/__emcore_armhelpers.o
|
| 34 | +
|
| 35 | +SRC := $(call preprocesspaths,SOURCES,-I. -I..)
|
| 36 | +OBJ := $(SRC:%.c=build/%.o)
|
| 37 | +OBJ := $(OBJ:%.S=build/%.o) $(HELPERS)
|
| 38 | +
|
| 39 | +all: $(NAME)
|
| 40 | +
|
| 41 | +-include $(OBJ:%=%.dep)
|
| 42 | +
|
| 43 | +$(NAME): build/$(NAME).emcoreapp
|
| 44 | +
|
| 45 | +build/$(NAME).emcoreapp: build/$(NAME).elf
|
| 46 | + @echo [EMCAPP] $<
|
| 47 | +ifeq ($(COMPRESS),true)
|
| 48 | + @$(ELF2ECA) -z -s $(STACKSIZE) -o $@ $^
|
| 49 | +else
|
| 50 | + @$(ELF2ECA) -s $(STACKSIZE) -o $@ $^
|
| 51 | +endif
|
| 52 | +
|
| 53 | +build/$(NAME).elf: ls.x $(OBJ)
|
| 54 | + @echo [LD] $@
|
| 55 | + @$(LD) $(LDFLAGS) -o $@ -T ls.x $(OBJ)
|
| 56 | +
|
| 57 | +build/%.o: %.c build/version.h
|
| 58 | + @echo [CC] $<
|
| 59 | +ifeq ($(shell uname),WindowsNT)
|
| 60 | + @-if not exist $(subst /,\,$(dir $@)) md $(subst /,\,$(dir $@))
|
| 61 | +else
|
| 62 | + @-mkdir -p $(dir $@)
|
| 63 | +endif
|
| 64 | + @$(CC) -c $(CFLAGS) -o $@ $<
|
| 65 | + @$(CC) -MM $(CFLAGS) $< > $@.dep.tmp
|
| 66 | + @sed -e "s|.*:|$@:|" < $@.dep.tmp > $@.dep
|
| 67 | +ifeq ($(shell uname),WindowsNT)
|
| 68 | + @sed -e "s/.*://" -e "s/\\$$//" < $@.dep.tmp | fmt -1 | sed -e "s/^ *//" -e "s/$$/:/" >> $@.dep
|
| 69 | +else
|
| 70 | + @sed -e 's/.*://' -e 's/\\$$//' < $@.dep.tmp | fmt -1 | sed -e 's/^ *//' -e 's/$$/:/' >> $@.dep
|
| 71 | +endif
|
| 72 | + @rm -f $@.dep.tmp
|
| 73 | +
|
| 74 | +build/%.o: %.S build/version.h
|
| 75 | + @echo [CC] $<
|
| 76 | +ifeq ($(shell uname),WindowsNT)
|
| 77 | + @-if not exist $(subst /,\,$(dir $@)) md $(subst /,\,$(dir $@))
|
| 78 | +else
|
| 79 | + @-mkdir -p $(dir $@)
|
| 80 | +endif
|
| 81 | + @$(CC) -c $(CFLAGS) -o $@ $<
|
| 82 | + @$(CC) -MM $(CFLAGS) $< > $@.dep.tmp
|
| 83 | + @sed -e "s|.*:|$@:|" < $@.dep.tmp > $@.dep
|
| 84 | +ifeq ($(shell uname),WindowsNT)
|
| 85 | + @sed -e "s/.*://" -e "s/\\$$//" < $@.dep.tmp | fmt -1 | sed -e "s/^ *//" -e "s/$$/:/" >> $@.dep
|
| 86 | +else
|
| 87 | + @sed -e 's/.*://' -e 's/\\$$//' < $@.dep.tmp | fmt -1 | sed -e 's/^ *//' -e 's/$$/:/' >> $@.dep
|
| 88 | +endif
|
| 89 | + @rm -f $@.dep.tmp
|
| 90 | +
|
| 91 | +build/__emcore_%.o: $(EMCOREDIR)/export/%.c
|
| 92 | + @echo [CC] $<
|
| 93 | +ifeq ($(shell uname),WindowsNT)
|
| 94 | + @-if not exist $(subst /,\,$(dir $@)) md $(subst /,\,$(dir $@))
|
| 95 | +else
|
| 96 | + @-mkdir -p $(dir $@)
|
| 97 | +endif
|
| 98 | + @$(CC) -c $(CFLAGS) -o $@ $<
|
| 99 | +
|
| 100 | +build/__emcore_%.o: $(EMCOREDIR)/export/%.S
|
| 101 | + @echo [CC] $<
|
| 102 | +ifeq ($(shell uname),WindowsNT)
|
| 103 | + @-if not exist $(subst /,\,$(dir $@)) md $(subst /,\,$(dir $@))
|
| 104 | +else
|
| 105 | + @-mkdir -p $(dir $@)
|
| 106 | +endif
|
| 107 | + @$(CC) -c $(CFLAGS) -o $@ $<
|
| 108 | +
|
| 109 | +build/version.h: version.h .svn/entries
|
| 110 | + @echo [PP] $<
|
| 111 | +ifeq ($(shell uname),WindowsNT)
|
| 112 | + @-if not exist build md build
|
| 113 | + @sed -e "s/\$$REVISION\$$/$(REVISION)/" -e "s/\$$REVISIONINT\$$/$(REVISIONINT)/" < $< > $@
|
| 114 | +else
|
| 115 | + @-mkdir -p build
|
| 116 | + @sed -e 's/\$$REVISION\$$/$(REVISION)/' -e 's/\$$REVISIONINT\$$/$(REVISIONINT)/' < $< > $@
|
| 117 | +endif
|
| 118 | +
|
| 119 | +clean:
|
| 120 | + @rm -rf build
|
| 121 | +
|
| 122 | +.PHONY: all clean $(NAME)
|
Index: apps/snake |
Property changes on: apps/snake |
___________________________________________________________________ |
Added: svn:ignore |
## -0,0 +1 ## |
| 123 | +build |