Commits

Ivan Vučica  committed fd8e17e

Tileset and tile pages.

  • Participants
  • Parent commits 2967cb0

Comments (0)

Files changed (8)

 
 CFLAGS += -Ilibpng-android/jni
 
-OBJS=src/catchdroid.o src/Texture.o src/Asset.o
+OBJS=src/catchdroid.o src/Texture.o src/Asset.o src/Page.o
 
 all: $(APKNAME).apk
 

File assets/pages/16-16

+00 08 09 10 11 12 00 00
+00 16 17 18 19 20 00 00
+00 00 00 01 00 00 00 00
+00 00 00 01 00 00 00 00
+00 00 00 01 00 00 00 00
+00 00 00 00 00 00 00 00
+00 00 02 02 02 02 00 00
+00 00 00 00 00 00 00 00 

File assets/tiles1.png

Added
New image
 @property (readonly, assign) size_t length;
 + (Asset *) assetWithPath: (NSString*)path;
 - (id) initWithPath: (NSString*)relativePath;
+- (NSString *) string;
 @end
 
     AAsset_close(_asset);
   [super dealloc];
 }
+
+- (NSString *) string
+{
+  return [[[NSString alloc] initWithBytes: [self buffer]
+                                   length: [self length]
+                                 encoding: NSUTF8StringEncoding] autorelease];
+}
 @end
 
+#import <Foundation/Foundation.h>
+#import <GLES/gl.h>
+@class Texture;
+
+#define PAGE_WIDTH 8
+#define PAGE_HEIGHT 8
+@interface Page : NSObject
+{
+  Texture * _tileset;
+  int _pageX, _pageY;
+
+  int _tiles[16*16];
+  GLfloat _textureCoordinates[2 * 6 * 16 * 16];
+}
+@property (nonatomic, retain) Texture * tileset;
+- (id) initWithPageX: (int)pageX 
+               pageY: (int)pageY;
+- (BOOL) loadFromFile: (NSString*) path;
+- (void) draw;
+@end
+#import "Page.h"
+#import "Texture.h"
+#import "Asset.h"
+
+#include <android/log.h>
+
+#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "native-activity", __VA_ARGS__))
+#define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "native-activity", __VA_ARGS__))
+
+#define TILESET_WIDTH 8
+#define TILESET_HEIGHT 8
+
+static GLfloat gridVertices[(2) * // x,y
+                            (6) * // 6 vertices per quad
+                            PAGE_WIDTH * PAGE_HEIGHT
+                           ];
+
+@implementation Page
+@synthesize tileset=_tileset;
+
++ (void) load
+{
+  [super load];
+
+  for(int j = 0; j < PAGE_HEIGHT; j++)
+  {
+    for(int i = 0; i < PAGE_WIDTH; i++)
+    {
+      static const GLfloat vertices[] = {
+        0, 0,
+        1, 0,
+        1, 1,
+
+        1, 1,
+        0, 1,
+        0, 0
+      };
+
+      for(int k = 0; k < 6; k++)
+      {
+        gridVertices[2 * 6 * PAGE_WIDTH * j +
+                     2 * 6 * i +
+                     2 * k + 0] = (i + vertices[2 * k + 0]) / ((GLfloat)PAGE_WIDTH);
+        gridVertices[2 * 6 * PAGE_WIDTH * j +
+                     2 * 6 * i +
+                     2 * k + 1] = (j + vertices[2 * k + 1]) / ((GLfloat)PAGE_HEIGHT);
+      }
+    }
+  }
+
+}
+- (id) initWithPageX: (int)pageX 
+               pageY: (int)pageY
+{
+  self = [super init];
+  if (!self) return nil;
+
+  BOOL success = [self loadFromFile: [NSString stringWithFormat: @"pages/%d-%d", pageX, pageY]];
+  if (!success)
+  {
+    [self release];
+    return nil;
+  }
+  LOGI("%g %g - %g %g - %g %g - %g %g", 
+      gridVertices[0], gridVertices[1],
+      gridVertices[2], gridVertices[3],
+      gridVertices[4], gridVertices[5],
+      gridVertices[6], gridVertices[7]);
+
+  return self;
+}
+- (void) dealloc
+{
+  [_tileset release];
+  [super dealloc];
+}
+
+- (BOOL) loadFromFile: (NSString*) path
+{
+
+  Asset * asset = [Asset assetWithPath: path];
+  if (!asset)
+  {
+    LOGW("Could not load asset %s", [path UTF8String]);
+    return NO;
+  }
+
+  NSString * s = [[asset string] stringByReplacingOccurrencesOfString: @"\n" withString: @" "];
+  NSArray * tiles = [s componentsSeparatedByString: @" "];
+  int i = 0;
+  for(NSString * tile in tiles)
+  {
+    if(sscanf([tile UTF8String], "%d", &_tiles[i]) != 1)
+      break;
+
+    int x = i % PAGE_WIDTH;
+    int y = PAGE_HEIGHT-1 - i / PAGE_WIDTH;
+
+    static const GLfloat textures[] = {
+      0, 1,
+      1, 1,
+      1, 0,
+
+      1, 0,
+      0, 0,
+      0, 1
+    };
+      LOGI("at %i (%d,%d): %d", x, y, _tiles[i]);
+    for(int k = 0; k < 6; k++)
+    {
+      _textureCoordinates[(2 * 6 * PAGE_WIDTH * y) +
+                          (2 * 6 * x) + 
+                          (2 * k + 0)] = (_tiles[i] % (TILESET_WIDTH) + textures[2*k + 0]) / ((GLfloat)TILESET_WIDTH); // 8 tiles per row in 128x128 tileset png
+      _textureCoordinates[(2 * 6 * PAGE_WIDTH * y) +
+                          (2 * 6 * x) + 
+                          (2 * k + 1)] = (_tiles[i] / (TILESET_HEIGHT) + textures[2*k + 1]) / ((GLfloat)TILESET_HEIGHT); 
+    }
+
+    i++;
+    if(i >= 16*16) break;
+  }
+  [self setTileset: [Texture textureWithPath: @"tiles1.png"]]; 
+  return YES;
+}
+- (void) draw
+{
+    glEnable(GL_TEXTURE_2D);
+    glEnableClientState(GL_VERTEX_ARRAY);
+    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
+  
+    glBindTexture(GL_TEXTURE_2D, [_tileset textureId]);
+    glVertexPointer(2, GL_FLOAT, 0, gridVertices);
+    glTexCoordPointer(2, GL_FLOAT, 0, _textureCoordinates);
+    glDrawArrays(GL_TRIANGLES, 0, 6 * (16 * 16));
+    
+    glDisableClientState(GL_VERTEX_ARRAY);
+    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
+    glDisable(GL_TEXTURE_2D);
+}
+@end

File src/catchdroid.m

 #include <Foundation/NSObject.h>
 
 #import "Texture.h"
+#import "Page.h"
 
 /**
  * Our saved state data.
 
     @private
     Texture * logo;
+    Page * page;
 }
 -(int)setupDisplay;
 @end
     glShadeModel(GL_SMOOTH);
     glDisable(GL_DEPTH_TEST);
 
+    page = [[Page alloc] initWithPageX: 16 pageY: 16];
+
     logo = [Texture textureWithPath: @"logo.png"];
     [logo retain];
 
 
     ////////////////////////
 
+    glPushMatrix();
+    [page draw];
+    glPopMatrix();
+
+    ////////////////////////
+
     glEnable(GL_TEXTURE_2D);
     glEnableClientState(GL_VERTEX_ARRAY);
     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
     };
     glVertexPointer(3, GL_FLOAT, 0, vertices);
     glTexCoordPointer(2, GL_FLOAT, 0, textures);
-    glDrawArrays(GL_TRIANGLES, 0, 6);
+    //glDrawArrays(GL_TRIANGLES, 0, 6);
 
     glDisableClientState(GL_VERTEX_ARRAY);
     glDisableClientState(GL_TEXTURE_COORD_ARRAY);
 -(void)terminateDisplay
 {
     LOGI("Terminating display");
+    [page release];
+    page = nil;
     [logo release];
     logo = nil;