Click here to Skip to main content
Click here to Skip to main content

LodePngWin32 Class for Rendering PNG Files in Raw WinAPI

, 23 Apr 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
C++ class that provides a convenient wrapper for lodepng library

Introduction

This project contains a class which provides a wrapper around the lodepng library provided by Lode Vandevenne. Its primary purpose is to provide tools to easily read and display PNG files, for C++ programmers who write in raw WinAPI.

Background

The PNG image format is an excellent format to use for images which are to be included in programs; it provides lossless compression which gives dramatically smaller files than BMP, with far better image quality than JPG. However, the normal WinAPI libraries cannot handle PNG files.

Recently, I was creating a game which used sprite-based graphics; I wrote it initially using the sprite images from the ancient Nethack game; it stored its sprites in a 1.2MB file containing over 1000 sprites! It worked well and was easy to use, but the same file in PNG format was 155KB ... much better!!

I started researching PNG libraries, and discovered Lode Vandevenne's LodePNG library, which comprised a single 6600-line C++ source file and a header. However, I had some problems with it. The first was that he did not have any examples which showed how to access his resulting data from a WinAPI application; all his rendering examples were OpenGL or SDL. Also, it required creating two vector objects for each file, which was awkward when several files were involved.

So, after doing some research online, I found a way to convert his BMP vector into an HBITMAP reference which can be used by BitBlt and other normal WinAPI GDI functions, and that worked quite well. With all the required tools in hand, I then wrapped all these functions into a simple class which makes access to lodepng quite trivial. The enclosed demo program will demonstrate how to use this library.

Using the Code

#include "lode_png.h"

//*****************************************************************
//  instantiating classes for the PNG files
//*****************************************************************
//  When loading multi-image (i.e., sprite) files,
//  specify the individual sprite width and height.
//  tiles32.png contains a 40x27-sprite array of sprite images
//****************************************************************/
#define  SPRITE_WIDTH      32
#define  SPRITE_HEIGHT     32
LodePngWin32 pngSprites("tiles32.png", SPRITE_HEIGHT, SPRITE_WIDTH) ;

//  for single-image file, only filename is required
LodePngWin32 pngFireCat("FireCat.png") ;

//*****************************************************************/
typedef struct sprite_sel_s {
   uint column ;
   uint row ;
} sprite_sel_t ;

//  select four arbitrary sprites from the tiles32 image file
static sprite_sel_t const sprite_selection[4] = {
{  6, 13 },   //  shield
{  8, 20 },   //  green gem
{ 23, 14 },   //  multi-colored amulet
{ 39,  0 },   //  smiling cat
} ;

//  For Window-based applications, render images in WM_PAINT.
//  For dialog-based applications, use WM_INITDIALOG
   case WM_PAINT:
      hdc = BeginPaint (hwnd, &ps);
      //  render some sprites
      pngSprites.render_bitmap(hdc,  50, 100, sprite_selection[0].column, sprite_selection[0].row) ;
      pngSprites.render_bitmap(hdc,  50, 200, sprite_selection[1].column, sprite_selection[1].row) ;
      pngSprites.render_bitmap(hdc, 120, 100, sprite_selection[2].column, sprite_selection[2].row) ;
      pngSprites.render_bitmap(hdc, 120, 200, sprite_selection[3].column, sprite_selection[3].row) ;

      //  render a single image
      pngFireCat.render_bitmap(hdc, 200,  50) ;
      EndPaint (hwnd, &ps);
      return 0;

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Derell Licht
Software Developer (Senior) Self-Employed
United States United States
I have worked for 25+ years as a firmware engineer, in various environments. In my private life, I create a variety of applications, including console applications in Linux and Windows, and GUI applications in Windows. All of my programs and code snippets are freeware, with source code available. All programs are written in C or C++, and built using the MinGW toolchain.

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.141015.1 | Last Updated 23 Apr 2014
Article Copyright 2014 by Derell Licht
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid