labyrinth

changeset 5:c8826e5ebec1

- changed every data loading function to return dummy objects instead of failing - fixed mistake in AndroidManifest.xml
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 01 May 2015 05:58:41 +0300
parents f35596f110bc
children 345db647383a
files android/AndroidManifest.xml android/Makefile android/build.xml src/level.c src/mesh.c src/texture.c src/timer.h
diffstat 7 files changed, 207 insertions(+), 13 deletions(-) [+]
line diff
     1.1 --- a/android/AndroidManifest.xml	Fri May 01 04:38:43 2015 +0300
     1.2 +++ b/android/AndroidManifest.xml	Fri May 01 05:58:41 2015 +0300
     1.3 @@ -7,15 +7,18 @@
     1.4  	<uses-sdk android:minSdkVersion="9"/>
     1.5  
     1.6  	<application android:label="labyrinth"
     1.7 -		android:hasCode="false"
     1.8 -		android:debuggable="true">
     1.9 +			android:hasCode="false"
    1.10 +			android:debuggable="true">
    1.11 +			<!-- android:icon="@drawable/ic_launcher" -->
    1.12  
    1.13 -		<activity android:name="android.app.NativeActivity" android:label="Labyrinth">
    1.14 +		<activity android:name="android.app.NativeActivity"
    1.15 +			android:label="Labyrinth">
    1.16 +
    1.17  			<meta-data android:name="android.app.lib_name" android:value="labyrinth"/>
    1.18  
    1.19  			<intent-filter>
    1.20 -				<action android:name="android.intent.action.MAIN"/>
    1.21 -				<action android:name="android.intent.category.LAUNCHER"/>
    1.22 +				<action android:name="android.intent.action.MAIN" />
    1.23 +				<category android:name="android.intent.category.LAUNCHER" />
    1.24  			</intent-filter>
    1.25  		</activity>
    1.26  	</application>
     2.1 --- a/android/Makefile	Fri May 01 04:38:43 2015 +0300
     2.2 +++ b/android/Makefile	Fri May 01 05:58:41 2015 +0300
     2.3 @@ -65,6 +65,10 @@
     2.4  install-release:
     2.5  	adb install -r $(apk-release)
     2.6  
     2.7 +.PHONY: uninstall
     2.8 +uninstall:
     2.9 +	adb uninstall $(pkg)
    2.10 +
    2.11  .PHONY: run
    2.12  run:
    2.13  	adb shell am start -n $(pkg)/$(act)
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/android/build.xml	Fri May 01 05:58:41 2015 +0300
     3.3 @@ -0,0 +1,92 @@
     3.4 +<?xml version="1.0" encoding="UTF-8"?>
     3.5 +<project name="labyrinth" default="help">
     3.6 +
     3.7 +    <!-- The local.properties file is created and updated by the 'android' tool.
     3.8 +         It contains the path to the SDK. It should *NOT* be checked into
     3.9 +         Version Control Systems. -->
    3.10 +    <property file="local.properties" />
    3.11 +
    3.12 +    <!-- The ant.properties file can be created by you. It is only edited by the
    3.13 +         'android' tool to add properties to it.
    3.14 +         This is the place to change some Ant specific build properties.
    3.15 +         Here are some properties you may want to change/update:
    3.16 +
    3.17 +         source.dir
    3.18 +             The name of the source directory. Default is 'src'.
    3.19 +         out.dir
    3.20 +             The name of the output directory. Default is 'bin'.
    3.21 +
    3.22 +         For other overridable properties, look at the beginning of the rules
    3.23 +         files in the SDK, at tools/ant/build.xml
    3.24 +
    3.25 +         Properties related to the SDK location or the project target should
    3.26 +         be updated using the 'android' tool with the 'update' action.
    3.27 +
    3.28 +         This file is an integral part of the build system for your
    3.29 +         application and should be checked into Version Control Systems.
    3.30 +
    3.31 +         -->
    3.32 +    <property file="ant.properties" />
    3.33 +
    3.34 +    <!-- if sdk.dir was not set from one of the property file, then
    3.35 +         get it from the ANDROID_HOME env var.
    3.36 +         This must be done before we load project.properties since
    3.37 +         the proguard config can use sdk.dir -->
    3.38 +    <property environment="env" />
    3.39 +    <condition property="sdk.dir" value="${env.ANDROID_HOME}">
    3.40 +        <isset property="env.ANDROID_HOME" />
    3.41 +    </condition>
    3.42 +
    3.43 +    <!-- The project.properties file is created and updated by the 'android'
    3.44 +         tool, as well as ADT.
    3.45 +
    3.46 +         This contains project specific properties such as project target, and library
    3.47 +         dependencies. Lower level build properties are stored in ant.properties
    3.48 +         (or in .classpath for Eclipse projects).
    3.49 +
    3.50 +         This file is an integral part of the build system for your
    3.51 +         application and should be checked into Version Control Systems. -->
    3.52 +    <loadproperties srcFile="project.properties" />
    3.53 +
    3.54 +    <!-- quick check on sdk.dir -->
    3.55 +    <fail
    3.56 +            message="sdk.dir is missing. Make sure to generate local.properties using 'android update project' or to inject it through the ANDROID_HOME environment variable."
    3.57 +            unless="sdk.dir"
    3.58 +    />
    3.59 +
    3.60 +    <!--
    3.61 +        Import per project custom build rules if present at the root of the project.
    3.62 +        This is the place to put custom intermediary targets such as:
    3.63 +            -pre-build
    3.64 +            -pre-compile
    3.65 +            -post-compile (This is typically used for code obfuscation.
    3.66 +                           Compiled code location: ${out.classes.absolute.dir}
    3.67 +                           If this is not done in place, override ${out.dex.input.absolute.dir})
    3.68 +            -post-package
    3.69 +            -post-build
    3.70 +            -pre-clean
    3.71 +    -->
    3.72 +    <import file="custom_rules.xml" optional="true" />
    3.73 +
    3.74 +    <!-- Import the actual build file.
    3.75 +
    3.76 +         To customize existing targets, there are two options:
    3.77 +         - Customize only one target:
    3.78 +             - copy/paste the target into this file, *before* the
    3.79 +               <import> task.
    3.80 +             - customize it to your needs.
    3.81 +         - Customize the whole content of build.xml
    3.82 +             - copy/paste the content of the rules files (minus the top node)
    3.83 +               into this file, replacing the <import> task.
    3.84 +             - customize to your needs.
    3.85 +
    3.86 +         ***********************
    3.87 +         ****** IMPORTANT ******
    3.88 +         ***********************
    3.89 +         In all cases you must update the value of version-tag below to read 'custom' instead of an integer,
    3.90 +         in order to avoid having your file be overridden by tools such as "android update project"
    3.91 +    -->
    3.92 +    <!-- version-tag: 1 -->
    3.93 +    <import file="${sdk.dir}/tools/ant/build.xml" />
    3.94 +
    3.95 +</project>
     4.1 --- a/src/level.c	Fri May 01 04:38:43 2015 +0300
     4.2 +++ b/src/level.c	Fri May 01 05:58:41 2015 +0300
     4.3 @@ -13,6 +13,18 @@
     4.4  
     4.5  #define IS_SOLID(x)	((x) == C_WALL)
     4.6  
     4.7 +#define DEF_LVL_SZ	5
     4.8 +static const char def_lvl_cells[DEF_LVL_SZ][DEF_LVL_SZ] = {
     4.9 +	"#####",
    4.10 +	"##x##",
    4.11 +	"#o o#",
    4.12 +	"# s #",
    4.13 +	"#####"
    4.14 +};
    4.15 +
    4.16 +
    4.17 +static int default_level(struct level *lvl);
    4.18 +
    4.19  
    4.20  void level_init(struct level *lvl)
    4.21  {
    4.22 @@ -22,6 +34,28 @@
    4.23  	lvl->floor_tex_scale = lvl->wall_tex_scale = lvl->ceil_tex_scale = 1.0;
    4.24  }
    4.25  
    4.26 +static int default_level(struct level *lvl)
    4.27 +{
    4.28 +	int i, j;
    4.29 +
    4.30 +	lvl->num_cells[0] = lvl->num_cells[1] = DEF_LVL_SZ;
    4.31 +
    4.32 +	for(i=0; i<DEF_LVL_SZ; i++) {
    4.33 +		for(j=0; j<DEF_LVL_SZ; j++) {
    4.34 +			char c = def_lvl_cells[i][j];
    4.35 +			lvl->cells[i][j] = c;
    4.36 +			if(c == 's') {
    4.37 +				lvl->start_pos[0] = j;
    4.38 +				lvl->start_pos[1] = i;
    4.39 +			} else if(c == 'x') {
    4.40 +				lvl->goal_pos[0] = j;
    4.41 +				lvl->goal_pos[1] = i;
    4.42 +			}
    4.43 +		}
    4.44 +	}
    4.45 +	return 0;
    4.46 +}
    4.47 +
    4.48  static void clean_line(char *buf)
    4.49  {
    4.50  	char *end = buf + strlen(buf) - 1;
    4.51 @@ -41,23 +75,23 @@
    4.52  
    4.53  	if(!(fp = fopen(fname, "r"))) {
    4.54  		fprintf(stderr, "failed to open file: %s\n", fname);
    4.55 -		return -1;
    4.56 +		return default_level(lvl);
    4.57  	}
    4.58  
    4.59  	if(!fgets(buf, sizeof buf, fp)) {
    4.60  		fprintf(stderr, "level file %s is empty\n", fname);
    4.61  		fclose(fp);
    4.62 -		return -1;
    4.63 +		return default_level(lvl);
    4.64  	}
    4.65  	if(sscanf(buf, "s %dx%d", size, size + 1) != 2) {
    4.66  		fprintf(stderr, "level file %s doesn't start with size definition\n", fname);
    4.67  		fclose(fp);
    4.68 -		return -1;
    4.69 +		return default_level(lvl);
    4.70  	}
    4.71  	if(size[0] > MAX_LEVEL_SIZE || size[1] > MAX_LEVEL_SIZE) {
    4.72  		fprintf(stderr, "level size %dx%d is larger than compile-time maximum (%d)\n", size[0], size[1], MAX_LEVEL_SIZE);
    4.73  		fclose(fp);
    4.74 -		return -1;
    4.75 +		return default_level(lvl);
    4.76  	}
    4.77  
    4.78  	lvl->num_cells[0] = size[0];
     5.1 --- a/src/mesh.c	Fri May 01 04:38:43 2015 +0300
     5.2 +++ b/src/mesh.c	Fri May 01 05:58:41 2015 +0300
     5.3 @@ -1,8 +1,34 @@
     5.4  #include <stdlib.h>
     5.5 +#include <assert.h>
     5.6  #include "opengl.h"
     5.7  #include "mesh.h"
     5.8  #include "objfile.h"
     5.9  
    5.10 +static struct vec3 def_verts[] = {
    5.11 +	{-1, -1, 1}, {1, -1, 1}, {1, 1, 1},		{-1, -1, 1}, {1, 1, 1}, {-1, 1, 1},
    5.12 +	{1, -1, 1}, {1, -1, -1}, {1, 1, -1},	{1, -1, 1}, {1, 1, -1}, {1, 1, 1},
    5.13 +	{1, -1, -1}, {-1, -1, -1}, {-1, 1, -1},	{1, -1, -1}, {-1, 1, -1}, {1, 1, -1},
    5.14 +	{-1, -1, -1}, {-1, -1, 1}, {-1, 1, 1},	{-1, -1, -1}, {-1, 1, 1}, {-1, 1, -1}
    5.15 +};
    5.16 +#define FACEATTR(x, y, z)	{x, y, z}, {x, y, z}, {x, y, z}, {x, y, z}, {x, y, z}, {x, y, z}
    5.17 +static struct vec3 def_norm[] = {
    5.18 +	FACEATTR(0, 0, 1),
    5.19 +	FACEATTR(1, 0, 0),
    5.20 +	FACEATTR(0, 0, -1),
    5.21 +	FACEATTR(-1, 0, 0)
    5.22 +};
    5.23 +static struct vec3 def_tc[] = {
    5.24 +	{0, 0, 0}, {1, 0, 0}, {1, 1, 0}, {0, 0, 0}, {1, 1, 0}, {0, 1, 0},
    5.25 +	{0, 0, 0}, {1, 0, 0}, {1, 1, 0}, {0, 0, 0}, {1, 1, 0}, {0, 1, 0},
    5.26 +	{0, 0, 0}, {1, 0, 0}, {1, 1, 0}, {0, 0, 0}, {1, 1, 0}, {0, 1, 0},
    5.27 +	{0, 0, 0}, {1, 0, 0}, {1, 1, 0}, {0, 0, 0}, {1, 1, 0}, {0, 1, 0}
    5.28 +};
    5.29 +
    5.30 +static struct mesh def_mesh = {
    5.31 +	def_verts, def_norm, def_tc,
    5.32 +	sizeof def_verts / sizeof *def_verts
    5.33 +};
    5.34 +
    5.35  /* use objfile to load mesh data (vertices, normals, and texcoords)
    5.36   * from a wavefront|OBJ file.
    5.37   */
    5.38 @@ -13,7 +39,7 @@
    5.39  	struct objfile *obj;
    5.40  
    5.41  	if(!(obj = objf_load(fname))) {
    5.42 -		return 0;
    5.43 +		return &def_mesh;
    5.44  	}
    5.45  	m = malloc(sizeof *m);
    5.46  	m->num_verts = objf_vertex_count(obj);
    5.47 @@ -44,7 +70,7 @@
    5.48  
    5.49  void free_mesh(struct mesh *m)
    5.50  {
    5.51 -	if(m) {
    5.52 +	if(m && m != &def_mesh) {
    5.53  		free(m->vert);
    5.54  		free(m->norm);
    5.55  		free(m->tc);
     6.1 --- a/src/texture.c	Fri May 01 04:38:43 2015 +0300
     6.2 +++ b/src/texture.c	Fri May 01 05:58:41 2015 +0300
     6.3 @@ -7,6 +7,8 @@
     6.4  #define GL_GENERATE_MIPMAP_SGIS 0x8191
     6.5  #endif
     6.6  
     6.7 +static unsigned int default_texture(void);
     6.8 +
     6.9  unsigned int load_texture(const char *fname)
    6.10  {
    6.11  	unsigned int tex;
    6.12 @@ -14,7 +16,7 @@
    6.13  
    6.14  	if(!(img = load_image(fname))) {
    6.15  		fprintf(stderr, "failed to load image: %s\n", fname);
    6.16 -		return 0;
    6.17 +		return default_texture();
    6.18  	}
    6.19  
    6.20  	glGenTextures(1, &tex);
    6.21 @@ -26,3 +28,36 @@
    6.22  	free_image(img);
    6.23  	return tex;
    6.24  }
    6.25 +
    6.26 +#define DEF_TEX_SZ	64
    6.27 +
    6.28 +static unsigned int default_texture(void)
    6.29 +{
    6.30 +	static unsigned int tex;
    6.31 +	static unsigned char pixels[DEF_TEX_SZ * DEF_TEX_SZ * 3];
    6.32 +
    6.33 +	if(!tex) {
    6.34 +		/* generate it */
    6.35 +		int i, j;
    6.36 +		unsigned char *pptr = pixels;
    6.37 +
    6.38 +		for(i=0; i<DEF_TEX_SZ; i++) {
    6.39 +			for(j=0; j<DEF_TEX_SZ; j++) {
    6.40 +				int c = ((i >> 3) & 1) == ((j >> 3) & 1);
    6.41 +
    6.42 +				*pptr++ = c ? 255 : 64;
    6.43 +				*pptr++ = 100;
    6.44 +				*pptr++ = c ? 64 : 255;
    6.45 +			}
    6.46 +		}
    6.47 +
    6.48 +		glGenTextures(1, &tex);
    6.49 +		glBindTexture(GL_TEXTURE_2D, tex);
    6.50 +		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    6.51 +		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    6.52 +		glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
    6.53 +		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, DEF_TEX_SZ, DEF_TEX_SZ, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
    6.54 +	}
    6.55 +
    6.56 +	return tex;
    6.57 +}
     7.1 --- a/src/timer.h	Fri May 01 04:38:43 2015 +0300
     7.2 +++ b/src/timer.h	Fri May 01 05:58:41 2015 +0300
     7.3 @@ -7,4 +7,4 @@
     7.4  double get_time_sec(void);
     7.5  void sleep_sec(double sec);
     7.6  
     7.7 -#endif	// TIMER_H_
     7.8 +#endif	/* TIMER_H_ */