/* Loads PNG using libpng */ static TLN_Bitmap LoadPNG (char *filename) { png_image image; TLN_Bitmap bitmap = NULL; /* Only the image structure version number needs to be set. */ memset (&image, 0, sizeof image); image.version = PNG_IMAGE_VERSION; if (png_image_begin_read_from_file (&image, filename)) { int src_line_size = image.width * PNG_IMAGE_PIXEL_SIZE(image.format); int lut_size = PNG_IMAGE_COLORMAP_SIZE(image); BYTE* data = malloc (src_line_size * image.height); BYTE* lut = malloc (lut_size); bitmap = TLN_CreateBitmap (image.width, image.height, PNG_IMAGE_PIXEL_SIZE(image.format)<<3); /* Change this to try different formats! If you set a colormap format * then you must also supply a colormap below. */ image.format = PNG_FORMAT_RGB_COLORMAP; if (png_image_finish_read (&image, NULL, data, 0, lut)) { BYTE *src, *dst; unsigned int c; png_image_free (&image); /* copy scanlines */ src = data; for (c=0; c<image.height; c++) { dst = TLN_GetBitmapPtr (bitmap, 0, c); memcpy (dst, src, src_line_size); src += src_line_size; } /* get palette */ { BYTE *src = lut; png_uint_32 c; TLN_Palette palette; palette = TLN_CreatePalette (image.colormap_entries); for (c=0; c<image.colormap_entries; c++) { TLN_SetPaletteColor (palette, c, src[0], src[1], src[2]); src += 3; } TLN_SetBitmapPalette (bitmap, palette); } } free (lut); free (data); } return bitmap; }
/*! * \brief * Loads a palette from a standard .act file * * \param filename * ACT file containing the palette to load * * \returns * A reference to the newly loaded palette, or NULL if error * * \remarks * Palettes are also automatically created when loading tilesets and spritesets. * Use the functions TLN_GetTilesetPalette() and TLN_GetSpritesetPalette() to retrieve them. * * \see * TLN_GetTilesetPalette(), TLN_GetSpritesetPalette() */ TLN_Palette TLN_LoadPalette (char *filename) { FILE *pf; TLN_Palette palette = NULL; int size; int c; /* open file */ pf = fopen (filename, "rb"); if (!pf) { TLN_SetLastError (TLN_ERR_FILE_NOT_FOUND); return NULL; } /* check size */ fseek (pf, 0, SEEK_END); size = ftell (pf); /* load trailing and get number of entries */ if (size == ACT_SIZE) { fseek (pf, - (int)sizeof(trailing), SEEK_END); fread (&trailing, sizeof(trailing), 1, pf); trailing.entries = SWAP(trailing.entries); trailing.transparent = SWAP(trailing.transparent); } else trailing.entries = size/3; /* create palette and load from file */ palette = TLN_CreatePalette (trailing.entries); fseek (pf, 0, SEEK_SET); for (c=0; c<trailing.entries; c++) { BYTE src[3]; fread (src, sizeof(src), 1, pf); TLN_SetPaletteColor (palette, c, src[0], src[1], src[2]); } fclose (pf); TLN_SetLastError (TLN_ERR_OK); return palette; }
/*! * \brief * Starts a palette animation * * \param index * Id of the animation to set (0 <= id < num_animations) * * \param palette * Reference of the palette to be animated * * \param sequence * Reference of the sequence to assign * * \param blend * true for smooth frame interpolation, false for classic, discrete mode */ bool TLN_SetPaletteAnimation (int index, TLN_Palette palette, TLN_Sequence sequence, bool blend) { Animation* animation; int c; struct Strip* strips; TLN_SetLastError (TLN_ERR_OK); if (index >= engine->numanimations) { TLN_SetLastError (TLN_ERR_IDX_ANIMATION); return false; } animation = &engine->animations[index]; if (animation->sequence == sequence) return true; /* validate type */ if (!CheckBaseObject (palette, OT_PALETTE) || !CheckBaseObject (sequence, OT_SEQUENCE)) return false; SetAnimation (animation, sequence, TYPE_PALETTE); animation->palette = palette; animation->blend = blend; /* start timers */ strips = (struct Strip*)&sequence->data; for (c=0; c<sequence->count; c++) { strips[c].timer = 0; strips[c].t0 = 0; } /* create auxiliary palette */ if (animation->srcpalette == NULL) animation->srcpalette = TLN_CreatePalette (256); CopyBaseObject (animation->srcpalette, palette); return true; }
JNIEXPORT jint JNICALL Java_Tilengine_CreatePalette (JNIEnv* env, jobject thisobj, jint entries) { return (jint)TLN_CreatePalette (entries); }
int main (int argc, char* argv[]) { TLN_Init (640,480,0,0,1); TLN_CreateWindow (NULL, CWF_VSYNC); TLN_EnableBlur (true); TLN_SetLoadPath ("cycles"); palette = TLN_CreatePalette (256); palette_int = TLN_CreatePalette (256); SetScene (pos); while (TLN_ProcessWindow ()) { if (TLN_GetInput (INPUT_LEFT)) { if (dec == false) { dec = true; pos = (pos - 1 + MAX_SCENE) % MAX_SCENE; SetScene (pos); } } else dec = false; if (TLN_GetInput (INPUT_RIGHT)) { if (inc == false) { inc = true; pos = (pos + 1) % MAX_SCENE; SetScene (pos); } } else inc = false; /* update palette */ if (!(frame & 0x01)) { TLN_Palette pal1, pal2; int t = frame % MAX_TIME; int idx1 = FindTimeIndex (t); int idx2 = (idx1 + 1) % maxpal; int t1 = palettes[idx1].time; int t2 = palettes[idx2].time; if (t1 > t2) { t2 += MAX_TIME; if (t < t1) t += MAX_TIME; } t -= t1; t2 -= t1; pal1 = palettes[idx1].palette; pal2 = palettes[idx2].palette; TLN_MixPalettes (pal1, pal2, palette_int, t*255/t2); /* rescale t2 to 255 */ SetPalette (idx1); } TLN_DrawFrame (frame); frame++; } TLN_DeleteBitmap (background); TLN_DeletePalette (palette); TLN_DeletePalette (palette_int); TLN_DeleteWindow (); TLN_Deinit (); return 0; }
/* loads BMP */ static TLN_Bitmap LoadBMP (char *filename) { BITMAPFILEHEADER bfh; BITMAPV5HEADER bv5; DWORD StructSize; FILE* pf; TLN_Bitmap bitmap = NULL; unsigned int c; int pitch; /* open file */ pf = fopen (filename, "rb"); if (!pf) return NULL; /* read BMP header */ fread (&bfh, sizeof(bfh), 1, pf); if (bfh.Type != 0x4D42) { fclose (pf); return NULL; } /* load info structure */ memset (&bv5, 0, sizeof(bv5)); fread (&StructSize, 4, 1, pf); fseek (pf, sizeof(bfh), SEEK_SET); fread (&bv5, StructSize, 1, pf); /* create */ bitmap = TLN_CreateBitmap (bv5.bV5Width, bv5.bV5Height, bv5.bV5BitCount); if (!bitmap) { fclose (pf); return NULL; } /* load scanlines */ pitch = TLN_GetBitmapPitch (bitmap); fseek (pf, bfh.OffsetData, SEEK_SET); for (c=0; c<bv5.bV5Height; c++) { BYTE* line = TLN_GetBitmapPtr (bitmap, 0, bv5.bV5Height - c - 1); fread (line, pitch, 1, pf); } /* load palette */ if (bv5.bV5BitCount == 8) { TLN_Palette palette; /* HACK: some editors don't set the bV5ClrUsed field, compute from size */ if (bv5.bV5ClrUsed == 0) bv5.bV5ClrUsed = (bfh.OffsetData - sizeof(bfh) - bv5.bV5Size) / sizeof(RGBQUAD); fseek (pf, sizeof(BITMAPFILEHEADER) + bv5.bV5Size, SEEK_SET); palette = TLN_CreatePalette (bv5.bV5ClrUsed); for (c=0; c<(int)bv5.bV5ClrUsed; c++) { RGBQUAD color; fread (&color, sizeof(RGBQUAD), 1, pf); TLN_SetPaletteColor (palette, c, color.r, color.g, color.b); } TLN_SetBitmapPalette (bitmap, palette); } fclose (pf); return bitmap; }