Permalink
Browse files

loading spritesheets as separate textures for low-end hardware; corre…

…cted move command parameters (move target was incorrect)
  • Loading branch information...
1 parent 0212ec1 commit 49dd36f3888cb9f089e84212b06aff6fab12847f @qwe321 qwe321 committed Aug 11, 2011
View
@@ -24,13 +24,16 @@ SOURCES= \
tcp_client_state.c \
tcp_server_state.c \
rand.c \
- critters/human.c
# game modules
SOURCES+= \
main.c \
camera.c \
- commands.c
+ commands.c \
+
+# critters
+SOURCES+= \
+ critters/human.c
OBJECTS=$(SOURCES:.c=.o)
View
60 blit.c
@@ -83,6 +83,50 @@ int blit_load_spritesheet(char *texname, char *mapname)
}
//-----------------------------------------------------------------------------
+int blit_load_spritesheet_split(char *texname, char *mapname)
+{
+ printf("Loading spritesheet '%s'... ", mapname);
+ Image img;
+ if (!image_load_from_file(&img, texname)) {
+ printf("error: cannot open image.\n");
+ return 0;
+ }
+
+ FILE *fp;
+
+ if ((fp = fopen(mapname, "rt")) == NULL) {
+ printf("error: cannot map file.\n");
+ return 0;
+ }
+
+ char namebuf[255];
+
+ while (!feof(fp)) {
+ Sprite *sprite = malloc(sizeof(Sprite));
+ fscanf(fp, "%s = %f %f %d %d",
+ namebuf,
+ &sprite->u, &sprite->v,
+ &sprite->width, &sprite->height);
+
+ Texture *tex = texture_from_image_part(&img, sprite->u, sprite->v, sprite->width, sprite->height);
+ sprite->texture = tex;
+
+ sprite->u = 0;
+ sprite->v = 0;
+ sprite->w = 1;
+ sprite->h = 1;
+
+ str_set(is.key_str, namebuf);
+ hashmap_ins(is.sprites, is.key_str, &sprite);
+ }
+
+ fclose(fp);
+ image_free(&img);
+ printf("OK\n");
+ return 1;
+}
+
+//-----------------------------------------------------------------------------
Sprite *blit_get_sprite(char *name)
{
str_set(is.key_str, name);
@@ -107,6 +151,7 @@ void blit_sprite_scaled(Sprite *spr, int x, int y, float s)
/* implementation is poor, but fast enough */
glBegin(GL_QUADS);
+ glColor3f(1, 1, 1);
glTexCoord2f(spr->u, spr->v);
glVertex2f(x, y);
glTexCoord2f(spr->u, spr->v + spr->h );
@@ -117,3 +162,18 @@ void blit_sprite_scaled(Sprite *spr, int x, int y, float s)
glVertex2f(x + s * spr->width, y);
glEnd();
}
+
+//-----------------------------------------------------------------------------
+void blit_line(int x0, int y0, int x1, int y1, uint32_t color)
+{
+ glDisable(GL_TEXTURE_2D);
+
+ glBegin(GL_LINES);
+ glColor3f(((color >> 16) & 255)/255.0, ((color >> 8) & 255)/255.0, (color & 255)/255.0);
+ glVertex2f(x0, y0);
+ glVertex2f(x1, y1);
+ glEnd();
+
+ glEnable(GL_TEXTURE_2D);
+}
+
View
3 blit.h
@@ -1,6 +1,7 @@
#ifndef __BLIT_H__
#define __BLIT_H__
+#include <stdint.h>
#include "texture.h"
typedef struct
@@ -17,8 +18,10 @@ typedef struct
void blit_startup(void);
Sprite *blit_load_sprite(char *texname);
int blit_load_spritesheet(char *texname, char *mapname);
+int blit_load_spritesheet_split(char *texname, char *mapname);
Sprite *blit_get_sprite(char *name);
void blit_sprite(Sprite *spr, int x, int y);
void blit_sprite_scaled(Sprite *spr, int x, int y, float s);
+void blit_line(int x0, int y0, int x1, int y1, uint32_t color);
#endif // __BLIT_H__
View
@@ -1,10 +1,13 @@
#ifndef __CRITTER_H__
-#include "commands.h"
#define __CRITTER_H__
-#define CRITTER_BASE CritterVTable* vtable;
+#include "commands.h"
+
+#define CRITTER_BASE \
+ CritterVTable* vtable;
-struct CritterVTable; typedef struct CritterVTable CritterVTable;
+struct CritterVTable;
+typedef struct CritterVTable CritterVTable;
typedef struct {
CRITTER_BASE
View
@@ -9,7 +9,8 @@
#define CRI_RUNNING 1
typedef struct {
- CRITTER_BASE float x;
+ CRITTER_BASE
+ float x;
float y;
float velocity;
@@ -25,6 +26,7 @@ typedef struct {
float move_y;
} Human;
+//-----------------------------------------------------------------------------
static void tick(Critter * c)
{
Human *cri = (Human *) c;
@@ -37,7 +39,7 @@ static void tick(Critter * c)
cri->face_x = cri->move_x - cri->x;
cri->face_y = cri->move_y - cri->y;
float len = sqrt(cri->face_x * cri->face_x + cri->face_y * cri->face_y);
- if (len < 0.1) {
+ if (len < 0.12) {
cri->state = CRI_IDLE;
}
cri->face_x /= len;
@@ -48,6 +50,7 @@ static void tick(Critter * c)
}
}
+//-----------------------------------------------------------------------------
static void draw(Critter * c, float time_delta)
{
Human *cri = (Human *) c;
@@ -59,15 +62,18 @@ static void draw(Critter * c, float time_delta)
} else if (cri->state == CRI_RUNNING) {
anim = isoanim_get("Nolty.Running");
}
+
wx -= isoanim_width(anim) / 2;
wy -= isoanim_height(anim) - isoanim_width(anim) / 4;
- isoanim_blit_frame(anim, wx, wy, cri->anim_time, cri->face_x, cri->face_y);
+ isoanim_blit_frame(anim, wx, wy, cri->anim_time, cri->face_x, cri->face_y);
}
+//-----------------------------------------------------------------------------
static void think(Critter * critter)
{
}
+//-----------------------------------------------------------------------------
static void order(Critter * c, Netcmd * command)
{
Human *cri = (Human *) c;
@@ -82,15 +88,17 @@ static void order(Critter * c, Netcmd * command)
}
}
+//-----------------------------------------------------------------------------
static void get_viewpoint(Critter * c, float *x, float *y)
{
Human *cri = (Human *) c;
*x = cri->x;
*y = cri->y;
}
+//-----------------------------------------------------------------------------
CritterVTable *vtable;
-void human_init()
+void human_init_vtable()
{
vtable = malloc(sizeof(CritterVTable));
vtable->tick = tick;
@@ -100,7 +108,8 @@ void human_init()
vtable->get_viewpoint = get_viewpoint;
}
-Critter *human_new(float x, float y)
+//-----------------------------------------------------------------------------
+Critter *new_human(float x, float y)
{
Human *h = (Human *) malloc(sizeof(Human));
h->vtable = vtable;
View
@@ -3,7 +3,7 @@
#include "critter.h"
-Critter* human_new(float x,float y);
-void human_init(void);
+Critter* new_human(float x,float y);
+void human_init_vtable(void);
#endif
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Deleted file not rendered
View
8 iso.c
@@ -94,15 +94,15 @@ void iso_world2screen(float x, float y, float *ox, float *oy)
//-----------------------------------------------------------------------------
void iso_screen2world(float x, float y, float *ox, float *oy)
{
- *ox = y / is.tile_height - x / is.tile_width + is.cam_x;
- *oy = x / is.tile_width + y / is.tile_height + is.cam_y;
+ *ox = (y-window_height()/2) / is.tile_height - (x-window_width()/2) / is.tile_width + is.cam_x;
+ *oy = (x-window_width()/2) / is.tile_width + (y-window_height()/2) / is.tile_height + is.cam_y;
}
//-----------------------------------------------------------------------------
void iso_snap_screen2world(float x, float y, float *ox, float *oy)
{
- *ox = round(y / is.tile_height - x / is.tile_width + is.cam_x);
- *oy = round(x / is.tile_width + y / is.tile_height + is.cam_y);
+ *ox = round((y-window_height()/2) / is.tile_height - (x-window_width()/2) / is.tile_width + is.cam_x);
+ *oy = round((x-window_width()/2) / is.tile_width + (y-window_height()/2) / is.tile_height + is.cam_y);
}
//-----------------------------------------------------------------------------
View
20 main.c
@@ -14,6 +14,7 @@
#include "blit.h"
#include "font.h"
#include "iso.h"
+#include "rand.h"
#include "commands.h"
#include "camera.h"
@@ -37,7 +38,8 @@ TileMap *tilemap_init(int w, int h)
map->tiles = NEWC(int, w * h);
map->width = w;
map->height = h;
- memset(map->tiles, 0, w * h * 4);
+ for (int i = 0; i < w * h; i++) map->tiles[i] = 1+rand_rand()%4;
+ //memset(map->tiles, 0, w * h * 4);
return map;
}
@@ -79,7 +81,7 @@ void usage()
Engine *engine_init()
{
- human_init();
+ human_init_vtable();
Engine *engine = malloc(sizeof(Engine));
engine->map = tilemap_init(100, 100);
return engine;
@@ -91,14 +93,16 @@ void engine_free(Engine * engine)
free(engine);
}
-Sprite *g_tileset[3];
+Sprite *g_tileset[12];
void load_assets()
{
g_tileset[0] = blit_load_sprite("./data/tiles/template.png");
- g_tileset[1] = blit_load_sprite("./data/tiles/grass1.png");
- g_tileset[2] = blit_load_sprite("./data/tiles/walln.png");
+ g_tileset[1] = blit_load_sprite("./data/tiles/grass0.png");
+ g_tileset[2] = blit_load_sprite("./data/tiles/grass1.png");
+ g_tileset[3] = blit_load_sprite("./data/tiles/grass2.png");
+ g_tileset[4] = blit_load_sprite("./data/tiles/grass3.png");
- blit_load_spritesheet("./data/SheetNolty.png", "./data/SheetNolty.txt");
+ blit_load_spritesheet_split("./data/SheetNolty.png", "./data/SheetNolty.txt");
font_load("./data/font/jura.png", "./data/font/jura.fnt");
font_load("./data/font/ubuntu.png", "./data/font/ubuntu.fnt");
@@ -123,7 +127,7 @@ void client_loop(NetworkType * network)
Camera *camera = camera_init();
for (int i = 0; i < MAX_CLIENTS; i++) {
- cri[i] = human_new(0, 0);
+ cri[i] = new_human(50, 50);
}
float time_step = 1.0 / 30.0;
@@ -146,7 +150,7 @@ void client_loop(NetworkType * network)
Netcmd_MoveCritter command;
command.header.type = NETCMD_MOVECRITTER;
command.sender = network->get_id(network->state);
- iso_screen2world(camera->x - window_width() / 2 + window_xmouse(), camera->y - window_height() / 2 + window_ymouse(), &command.move_x, &command.move_y);
+ iso_screen2world(window_xmouse(), window_ymouse(), &command.move_x, &command.move_y);
network->add_command(network->state, (void *)&command);
}
View
2 rand.h
@@ -1,6 +1,8 @@
#ifndef __RAND_H__
#define __RAND_H__
+
int rand_rand_r(unsigned int *ctx);
int rand_rand(void);
void rand_set_seed(unsigned int seed);
+
#endif
View
@@ -54,8 +54,9 @@ void image_free(Image *img)
}
//-----------------------------------------------------------------------------
-int texture_from_image(Texture *tex, Image *img)
+Texture *texture_from_image(Image *img)
{
+ Texture *tex = malloc(sizeof(Texture));
tex->width = img->width;
tex->height = img->height;
glGenTextures(1, &tex->handle);
@@ -66,7 +67,31 @@ int texture_from_image(Texture *tex, Image *img)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- return 1;
+ return tex;
+}
+
+//-----------------------------------------------------------------------------
+Texture *texture_from_image_part(Image *img, int u, int v, int w, int h)
+{
+ Texture *tex = malloc(sizeof(Texture));
+ tex->width = w;
+ tex->height = h;
+ glGenTextures(1, &tex->handle);
+ glBindTexture(GL_TEXTURE_2D, tex->handle);
+ int bpp = img->has_alpha ? 4 : 3;
+ unsigned char *img_part = malloc(w * h * bpp);
+ unsigned char *img_start = img->pixels + (v*img->width + u)*bpp;
+ for (int i = 0; i < h; i++) {
+ memcpy(img_part + i*w*bpp, img_start + i*img->width*bpp, w*bpp);
+ }
+ glTexImage2D(GL_TEXTURE_2D, 0, bpp, w, h,
+ 0, img->has_alpha ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE, img_part);
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+ free(img_part);
+ return tex;
}
//-----------------------------------------------------------------------------
@@ -75,8 +100,7 @@ Texture *texture_from_file(char *fname)
Image img;
if (image_load_from_file(&img, fname)) {
- Texture *tex = malloc(sizeof(Texture));
- texture_from_image(tex, &img);
+ Texture *tex = texture_from_image(&img);
image_free(&img);
return tex;
} else {
View
@@ -19,7 +19,8 @@ typedef struct
int image_load_from_file(Image *img, char *name);
void image_free(Image *img);
-int texture_from_image(Texture *tex, Image *img);
+Texture *texture_from_image(Image *img);
+Texture *texture_from_image_part(Image *img, int u, int v, int w, int h);
Texture* texture_from_file(char* name);
void texture_bind(Texture *tex);

0 comments on commit 49dd36f

Please sign in to comment.