Help with loading PCX images.

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
MysticWhiteDragon
Posts: 30
Joined: Thu Feb 16, 2006 8:46 am

Help with loading PCX images.

Post by MysticWhiteDragon »

I'm not sure why my image is not the right size and why it's all pink. Can someone help mw out with loading a PCX image? I'm somewhat of a beginner with programming.

main.c

Code: Select all

#include <pspdisplay.h>
#include <pspctrl.h>
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspgu.h>
#include <stdio.h>
#include "graphics.h"

int main&#40;&#41;
&#123;
  char buffer&#91;200&#93;;
  BITMAP* image;
  
  pspDebugScreenInit&#40;&#41;;
  initGraphics&#40;&#41;;
  
  sprintf&#40;buffer, "title.pcx"&#41;;
  image = load_pcx&#40;buffer,0&#41;;
  
  if &#40;!image&#41;
  &#123;
    //Image load failed
    printf&#40;"Image load failed!\n"&#41;;
  &#125;
  else
  &#123;
    int x = 0;
    int y = 0;
    sceDisplayWaitVblankStart&#40;&#41;;
    blitImageToScreen&#40;0,0,300,200,image,x,y&#41;;
    flipScreen&#40;&#41;;
  &#125;
  sceKernelSleepThread&#40;&#41;;
  return 0;
&#125;

graphics.c

Code: Select all

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <pspdisplay.h>
#include <psputils.h>
#include <pspgu.h>

#include "graphics.h"
Color* g_vram_base = &#40;Color*&#41; &#40;0x40000000 | 0x04000000&#41;;

#define IS_ALPHA&#40;color&#41; &#40;&#40;&#40;color&#41;&0xff000000&#41;==0xff000000?0&#58;1&#41;
#define FRAMEBUFFER_SIZE &#40;PSP_LINE_SIZE*SCREEN_HEIGHT*4&#41;
#define MAX&#40;X, Y&#41; &#40;&#40;X&#41; > &#40;Y&#41; ? &#40;X&#41; &#58; &#40;Y&#41;&#41;

BITMAP* screen = NULL;
BITMAP* buffer = NULL;

typedef struct
&#123;
	unsigned short u, v;
	short x, y, z;
&#125; Vertex;

extern u8 msx&#91;&#93;;

unsigned int __attribute__&#40;&#40;aligned&#40;16&#41;&#41;&#41; list&#91;262144&#93;;
static int dispBufferNumber;
static int initialized = 0;

static int getNextPower2&#40;int width&#41;
&#123;
	int b = width;
	int n;
	for &#40;n = 0; b != 0; n++&#41; b >>= 1;
	b = 1 << n;
	if &#40;b == 2 * width&#41; b >>= 1;
	return b;
&#125;

Color* getVramDrawBuffer&#40;&#41;
&#123;
	Color* vram = &#40;Color*&#41; g_vram_base;
	if &#40;dispBufferNumber == 0&#41; vram += FRAMEBUFFER_SIZE / sizeof&#40;Color&#41;;
	return vram;
&#125;

Color* getVramDisplayBuffer&#40;&#41;
&#123;
	Color* vram = &#40;Color*&#41; g_vram_base;
	if &#40;dispBufferNumber == 1&#41; vram += FRAMEBUFFER_SIZE / sizeof&#40;Color&#41;;
	return vram;
&#125;

BITMAP *load_pcx&#40;const char* filename,PALETTE pal&#41;
&#123;
  FILE *df;
  unsigned long offset =4;
  int w = 0,h = 0;
  int c,i,j,numRepeat;
  unsigned char xMin,yMin,xMax,yMax;
  unsigned char *line,*pixData = NULL,*palData;

  BITMAP* pcx = &#40;BITMAP*&#41; malloc&#40;sizeof&#40;BITMAP&#41;&#41;;
  if &#40;!pcx&#41; return NULL;

  if &#40;&#40;df = fopen&#40;filename, "rb"&#41;&#41; == NULL&#41; return NULL;

  rewind&#40;df&#41;;
  fgetc&#40;df&#41;;
  fgetc&#40;df&#41;;
  fgetc&#40;df&#41;;
  fgetc&#40;df&#41;;

  xMin = fgetc&#40;df&#41;; xMin |= fgetc&#40;df&#41; << 8;
  yMin = fgetc&#40;df&#41;; yMin |= fgetc&#40;df&#41; << 8;
  xMax = fgetc&#40;df&#41;; xMax |= fgetc&#40;df&#41; << 8;
  yMax = fgetc&#40;df&#41;; yMax |= fgetc&#40;df&#41; << 8;
  
  pcx->imageWidth  = xMax - xMin + 1;
  pcx->imageHeight = yMax - yMin + 1;

  offset+=53;
  offset+=60;
  line = &#40;unsigned char*&#41;malloc&#40;w*h&#41;;
  fseek&#40;df, 128, SEEK_SET&#41;;
  pcx = create_bitmap&#40;w,h&#41;;
  for &#40;i = 0;i < &#40;w*h&#41;;i++&#41;
  &#123;
    c = getc&#40;df&#41;;
    if &#40;c > 0xbf&#41;
    &#123;
      numRepeat = 0x3f & c;
      c = getc&#40;df&#41;;
      for &#40;j = 0;j < numRepeat;j++&#41;
      &#123;
        pixData&#91;i++&#93; = c;
      &#125;
    &#125;
    else
      pixData&#91;i++&#93; = c;
    fflush&#40;stdout&#41;;
  &#125;
  palData = &#40;unsigned char*&#41;malloc&#40;768&#41;;
  fseek&#40;df, -769, SEEK_END&#41;;
  
  c = getc&#40;df&#41;;
  for &#40;i = 0; i < 768; i++&#41;
  &#123;
    c = getc&#40;df&#41;;
    palData&#91;i&#93; = c;
  &#125;
  
  free&#40;line&#41;;
  fclose&#40;df&#41;;
  return pcx;
&#125;

void blitImageToImage&#40;int sx, int sy, int width, int height, BITMAP* source, int dx, int dy, BITMAP* destination&#41;
&#123;
	Color* destinationData = &destination->data&#91;destination->textureWidth * dy + dx&#93;;
	int destinationSkipX = destination->textureWidth - width;
	Color* sourceData = &source->data&#91;source->textureWidth * sy + sx&#93;;
	int sourceSkipX = source->textureWidth - width;
	int x, y;
	for &#40;y = 0; y < height; y++, destinationData += destinationSkipX, sourceData += sourceSkipX&#41; &#123;
		for &#40;x = 0; x < width; x++, destinationData++, sourceData++&#41; &#123;
			*destinationData = *sourceData;
		&#125;
	&#125;
&#125;

void blitImageToScreen&#40;int sx, int sy, int width, int height, BITMAP* source, int dx, int dy&#41;
&#123;
	if &#40;!initialized&#41; return;
	Color* vram = getVramDrawBuffer&#40;&#41;;
	sceKernelDcacheWritebackInvalidateAll&#40;&#41;;
	guStart&#40;&#41;;
	sceGuCopyImage&#40;GU_PSM_8888, sx, sy, width, height, source->textureWidth, source->data, dx, dy, PSP_LINE_SIZE, vram&#41;;
	sceGuFinish&#40;&#41;;
	sceGuSync&#40;0,0&#41;;
&#125;

void blit&#40;BITMAP *source, BITMAP *dest, int source_x, int source_y, int dest_x, int dest_y, int width, int height&#41;
&#123;
  if &#40;dest->data==getVramDisplayBuffer&#40;&#41;&#41;
  &#123;
    blitImageToScreen&#40;source_x,source_y,width,height,source,dest_x,dest_y&#41;;
  &#125;
  else
  &#123;
    blitImageToImage&#40;source_x,source_y,width,height,source,dest_x,dest_y,dest&#41;;
  &#125;
&#125;

void blitAlphaImageToImage&#40;int sx, int sy, int width, int height, BITMAP* source, int dx, int dy, BITMAP* destination&#41;
&#123;
	// TODO Blend!
    Color* destinationData = &destination->data&#91;destination->textureWidth * dy + dx&#93;;
	int destinationSkipX = destination->textureWidth - width;
	Color* sourceData = &source->data&#91;source->textureWidth * sy + sx&#93;;
	int sourceSkipX = source->textureWidth - width;
	int x, y;
	for &#40;y = 0; y < height; y++, destinationData += destinationSkipX, sourceData += sourceSkipX&#41; &#123;
		for &#40;x = 0; x < width; x++, destinationData++, sourceData++&#41; &#123;
			Color color = *sourceData;
			if &#40;!IS_ALPHA&#40;color&#41;&#41; *destinationData = color;
		&#125;
	&#125;
&#125;

void blitAlphaImageToScreen&#40;int sx, int sy, int width, int height, BITMAP* source, int dx, int dy&#41;
&#123;
	if &#40;!initialized&#41; return;

	sceKernelDcacheWritebackInvalidateAll&#40;&#41;;
	guStart&#40;&#41;;
	sceGuTexImage&#40;0, source->textureWidth, source->textureHeight, source->textureWidth, &#40;void*&#41; source->data&#41;;
	float u = 1.0f / &#40;&#40;float&#41;source->textureWidth&#41;;
	float v = 1.0f / &#40;&#40;float&#41;source->textureHeight&#41;;
	sceGuTexScale&#40;u, v&#41;;
	
	int j = 0;
	while &#40;j < width&#41; &#123;
		Vertex* vertices = &#40;Vertex*&#41; sceGuGetMemory&#40;2 * sizeof&#40;Vertex&#41;&#41;;
		int sliceWidth = 64;
		if &#40;j + sliceWidth > width&#41; sliceWidth = width - j;
		vertices&#91;0&#93;.u = sx + j;
		vertices&#91;0&#93;.v = sy;
		vertices&#91;0&#93;.x = dx + j;
		vertices&#91;0&#93;.y = dy;
		vertices&#91;0&#93;.z = 0;
		vertices&#91;1&#93;.u = sx + j + sliceWidth;
		vertices&#91;1&#93;.v = sy + height;
		vertices&#91;1&#93;.x = dx + j + sliceWidth;
		vertices&#91;1&#93;.y = dy + height;
		vertices&#91;1&#93;.z = 0;
		sceGuDrawArray&#40;GU_SPRITES, GU_TEXTURE_16BIT | GU_VERTEX_16BIT | GU_TRANSFORM_2D, 2, 0, vertices&#41;;
		j += sliceWidth;
	&#125;
	
	sceGuFinish&#40;&#41;;
	sceGuSync&#40;0, 0&#41;;
&#125;

BITMAP* create_bitmap&#40;int width, int height&#41;
&#123;
	BITMAP* image = &#40;BITMAP*&#41; malloc&#40;sizeof&#40;BITMAP&#41;&#41;;
	if &#40;!image&#41; return NULL;
	image->imageWidth = width;
	image->imageHeight = height;
	image->textureWidth = getNextPower2&#40;width&#41;;
	image->textureHeight = getNextPower2&#40;height&#41;;
	image->data = &#40;Color*&#41; memalign&#40;16, image->textureWidth * image->textureHeight * sizeof&#40;Color&#41;&#41;;
	if &#40;!image->data&#41; return NULL;
	memset&#40;image->data, 0, image->textureWidth * image->textureHeight * sizeof&#40;Color&#41;&#41;;
	return image;
&#125;

void destroy_bitmap&#40;BITMAP* image&#41;
&#123;
	free&#40;image->data&#41;;
	free&#40;image&#41;;
&#125;

void clearImage&#40;Color color, BITMAP* image&#41;
&#123;
	int i;
	int size = image->textureWidth * image->textureHeight;
	Color* data = image->data;
	for &#40;i = 0; i < size; i++, data++&#41; *data = color;
&#125;

void clearScreen&#40;Color color&#41;
&#123;
	if &#40;!initialized&#41; return;
	guStart&#40;&#41;;
	sceGuClearDepth&#40;0&#41;;
	sceGuClear&#40;GU_COLOR_BUFFER_BIT|GU_DEPTH_BUFFER_BIT&#41;;
	sceGuFinish&#40;&#41;;
	sceGuSync&#40;0, 0&#41;;
&#125;

void clear_to_color&#40;BITMAP *bitmap,Color color&#41;
&#123;
  if &#40;bitmap->data==getVramDisplayBuffer&#40;&#41;&#41;
  &#123;
    clearScreen&#40;color&#41;;
  &#125;
  else
  &#123;
    clearImage&#40;color,bitmap&#41;;
  &#125;
&#125;

void fillImageRect&#40;Color color, int x0, int y0, int width, int height, BITMAP* image&#41;
&#123;
	int skipX = image->textureWidth - width;
	int x, y;
	Color* data = image->data + x0 + y0 * image->textureWidth;
	for &#40;y = 0; y < height; y++, data += skipX&#41; &#123;
		for &#40;x = 0; x < width; x++, data++&#41; *data = color;
	&#125;
&#125;

void fillScreenRect&#40;Color color, int x0, int y0, int width, int height&#41;
&#123;
	if &#40;!initialized&#41; return;
	int skipX = PSP_LINE_SIZE - width;
	int x, y;
	Color* data = getVramDrawBuffer&#40;&#41; + x0 + y0 * PSP_LINE_SIZE;
	for &#40;y = 0; y < height; y++, data += skipX&#41; &#123;
		for &#40;x = 0; x < width; x++, data++&#41; *data = color;
	&#125;
&#125;

void rectfill&#40;BITMAP *bmp,int x1, int y1, int x2, int y2, Color color&#41;
&#123;
  int width,height;
  
  width=x1-x2;
  height=y1-y2;
  
  if &#40;bmp->data==getVramDisplayBuffer&#40;&#41;&#41;
  &#123;
    fillScreenRect&#40;color,x1,y1,width,height&#41;;
  &#125;
  else
  &#123;
    fillImageRect&#40;color,x1,y1,width,height,bmp&#41;;
  &#125;
&#125;

void putPixelScreen&#40;Color color, int x, int y&#41;
&#123;
	Color* vram = getVramDrawBuffer&#40;&#41;;
	vram&#91;PSP_LINE_SIZE * y + x&#93; = color;
&#125;

void putPixelImage&#40;Color color, int x, int y, BITMAP* image&#41;
&#123;
	image->data&#91;x + y * image->textureWidth&#93; = color;
&#125;

void putpixel&#40;BITMAP *bmp, int x, int y, Color color&#41;
&#123;
  if &#40;bmp->data==getVramDisplayBuffer&#40;&#41;&#41;
  &#123;
    putPixelScreen&#40;color,x,y&#41;;
  &#125;
  else
  &#123;
    putPixelImage&#40;color,x,y,bmp&#41;;
  &#125;
&#125;

Color getPixelScreen&#40;int x, int y&#41;
&#123;
	Color* vram = getVramDrawBuffer&#40;&#41;;
	return vram&#91;PSP_LINE_SIZE * y + x&#93;;
&#125;

Color getPixelImage&#40;int x, int y, BITMAP* image&#41;
&#123;
	return image->data&#91;x + y * image->textureWidth&#93;;
&#125;

void getpixel&#40;BITMAP *bmp, int x, int y&#41;
&#123;
  if &#40;bmp->data==getVramDisplayBuffer&#40;&#41;&#41;
  &#123;
    getPixelScreen&#40;x,y&#41;;
  &#125;
  else
  &#123;
    getPixelImage&#40;x,y,bmp&#41;;
  &#125;
&#125;

void printTextScreen&#40;int x, int y, const char* text, u32 color&#41;
&#123;
	int c, i, j, l;
	u8 *font;
	Color *vram_ptr;
	Color *vram;
	
	if &#40;!initialized&#41; return;

	for &#40;c = 0; c < strlen&#40;text&#41;; c++&#41; &#123;
		if &#40;x < 0 || x + 8 > SCREEN_WIDTH || y < 0 || y + 8 > SCREEN_HEIGHT&#41; break;
		char ch = text&#91;c&#93;;
		vram = getVramDrawBuffer&#40;&#41; + x + y * PSP_LINE_SIZE;
		
		font = &msx&#91; &#40;int&#41;ch * 8&#93;;
		for &#40;i = l = 0; i < 8; i++, l += 8, font++&#41; &#123;
			vram_ptr  = vram;
			for &#40;j = 0; j < 8; j++&#41; &#123;
				if &#40;&#40;*font & &#40;128 >> j&#41;&#41;&#41; *vram_ptr = color;
				vram_ptr++;
			&#125;
			vram += PSP_LINE_SIZE;
		&#125;
		x += 8;
	&#125;
&#125;

void printTextImage&#40;int x, int y, const char* text, u32 color, BITMAP* image&#41;
&#123;
	int c, i, j, l;
	u8 *font;
	Color *data_ptr;
	Color *data;
	
	if &#40;!initialized&#41; return;

	for &#40;c = 0; c < strlen&#40;text&#41;; c++&#41; &#123;
		if &#40;x < 0 || x + 8 > image->imageWidth || y < 0 || y + 8 > image->imageHeight&#41; break;
		char ch = text&#91;c&#93;;
		data = image->data + x + y * image->textureWidth;
		
		font = &msx&#91; &#40;int&#41;ch * 8&#93;;
		for &#40;i = l = 0; i < 8; i++, l += 8, font++&#41; &#123;
			data_ptr  = data;
			for &#40;j = 0; j < 8; j++&#41; &#123;
				if &#40;&#40;*font & &#40;128 >> j&#41;&#41;&#41; *data_ptr = color;
				data_ptr++;
			&#125;
			data += image->textureWidth;
		&#125;
		x += 8;
	&#125;
&#125;

void saveImage&#40;const char* filename, Color* data, int width, int height, int lineSize, int saveAlpha&#41;
&#123;
  
&#125;

//int save_bitmap&#40;const char *filename, BITMAP *bmp,const RGB *pal&#41;
//&#123;
//  if &#40;bmp->data==getVramDisplayBuffer&#40;&#41;&#41;
//  &#123;
//    return saveImage&#40;filename,getVramDisplayBuffer&#40;&#41;,SCREEN_WIDTH,SCREEN_HEIGHT,PSP_LINE_SIZE,0&#41;;
//  &#125;
//  else
//  &#123;
//    return saveImage&#40;filename,bmp->data,bmp->imageWidth,bmp->imageHeight,bmp->textureWidth,0&#41;;
//  &#125;
//&#125;

void flipScreen&#40;&#41;
&#123;
	if &#40;!initialized&#41; return;
	sceGuSwapBuffers&#40;&#41;;
	dispBufferNumber ^= 1;
&#125;

static void drawLine&#40;int x0, int y0, int x1, int y1, int color, Color* destination, int width&#41;
&#123;
	int dy = y1 - y0;
	int dx = x1 - x0;
	int stepx, stepy;
	
	if &#40;dy < 0&#41; &#123; dy = -dy;  stepy = -width; &#125; else &#123; stepy = width; &#125;
	if &#40;dx < 0&#41; &#123; dx = -dx;  stepx = -1; &#125; else &#123; stepx = 1; &#125;
	dy <<= 1;
	dx <<= 1;
	
	y0 *= width;
	y1 *= width;
	destination&#91;x0+y0&#93; = color;
	if &#40;dx > dy&#41; &#123;
		int fraction = dy - &#40;dx >> 1&#41;;
		while &#40;x0 != x1&#41; &#123;
			if &#40;fraction >= 0&#41; &#123;
				y0 += stepy;
				fraction -= dx;
			&#125;
			x0 += stepx;
			fraction += dy;
			destination&#91;x0+y0&#93; = color;
		&#125;
	&#125; else &#123;
		int fraction = dx - &#40;dy >> 1&#41;;
		while &#40;y0 != y1&#41; &#123;
			if &#40;fraction >= 0&#41; &#123;
				x0 += stepx;
				fraction -= dy;
			&#125;
			y0 += stepy;
			fraction += dx;
			destination&#91;x0+y0&#93; = color;
		&#125;
	&#125;
&#125;

void drawLineScreen&#40;int x0, int y0, int x1, int y1, Color color&#41;
&#123;
	drawLine&#40;x0, y0, x1, y1, color, getVramDrawBuffer&#40;&#41;, PSP_LINE_SIZE&#41;;
&#125;

void drawLineImage&#40;int x0, int y0, int x1, int y1, Color color, BITMAP* image&#41;
&#123;
	drawLine&#40;x0, y0, x1, y1, color, image->data, image->textureWidth&#41;;
&#125;

void line&#40;BITMAP *bmp, int x1, int y1, int x2, int y2, Color color&#41;
&#123;
  if &#40;bmp->data==getVramDisplayBuffer&#40;&#41;&#41;
  &#123;
    drawLineScreen&#40;x1,y1,x2,y2,color&#41;;
  &#125;
  else
  &#123;
    drawLineImage&#40;x1,y1,x2,y2,color,bmp&#41;;
  &#125;
&#125;

#define BUF_WIDTH &#40;512&#41;
#define SCR_WIDTH &#40;480&#41;
#define SCR_HEIGHT &#40;272&#41;
#define PIXEL_SIZE &#40;4&#41; /* change this if you change to another screenmode */
#define FRAME_SIZE &#40;BUF_WIDTH * SCR_HEIGHT * PIXEL_SIZE&#41;
#define ZBUF_SIZE &#40;BUF_WIDTH SCR_HEIGHT * 2&#41; /* zbuffer seems to be 16-bit? */

void initGraphics&#40;&#41;
&#123;
	dispBufferNumber = 0;

	sceGuInit&#40;&#41;;

	guStart&#40;&#41;;
	sceGuDrawBuffer&#40;GU_PSM_8888, &#40;void*&#41;FRAMEBUFFER_SIZE, PSP_LINE_SIZE&#41;;
	sceGuDispBuffer&#40;SCREEN_WIDTH, SCREEN_HEIGHT, &#40;void*&#41;0, PSP_LINE_SIZE&#41;;
	sceGuClear&#40;GU_COLOR_BUFFER_BIT | GU_DEPTH_BUFFER_BIT&#41;;
	sceGuDepthBuffer&#40;&#40;void*&#41; &#40;FRAMEBUFFER_SIZE*2&#41;, PSP_LINE_SIZE&#41;;
	sceGuOffset&#40;2048 - &#40;SCREEN_WIDTH / 2&#41;, 2048 - &#40;SCREEN_HEIGHT / 2&#41;&#41;;
	sceGuViewport&#40;2048, 2048, SCREEN_WIDTH, SCREEN_HEIGHT&#41;;
	sceGuDepthRange&#40;0xc350, 0x2710&#41;;
	sceGuScissor&#40;0, 0, SCREEN_WIDTH, SCREEN_HEIGHT&#41;;
	sceGuEnable&#40;GU_SCISSOR_TEST&#41;;
	sceGuAlphaFunc&#40;GU_GREATER, 0, 0xff&#41;;
	sceGuEnable&#40;GU_ALPHA_TEST&#41;;
	sceGuDepthFunc&#40;GU_GEQUAL&#41;;
	sceGuEnable&#40;GU_DEPTH_TEST&#41;;
	sceGuFrontFace&#40;GU_CW&#41;;
	sceGuShadeModel&#40;GU_SMOOTH&#41;;
	sceGuEnable&#40;GU_CULL_FACE&#41;;
	sceGuEnable&#40;GU_TEXTURE_2D&#41;;
	sceGuEnable&#40;GU_CLIP_PLANES&#41;;
	sceGuTexMode&#40;GU_PSM_8888, 0, 0, 0&#41;;
	sceGuTexFunc&#40;GU_TFX_REPLACE, GU_TCC_RGBA&#41;;
	sceGuTexFilter&#40;GU_NEAREST, GU_NEAREST&#41;;
	sceGuAmbientColor&#40;0xffffffff&#41;;
	sceGuEnable&#40;GU_BLEND&#41;;
	sceGuBlendFunc&#40;GU_ADD, GU_SRC_ALPHA, GU_ONE_MINUS_SRC_ALPHA, 0, 0&#41;;
	sceGuFinish&#40;&#41;;
	sceGuSync&#40;0, 0&#41;;

	sceDisplayWaitVblankStart&#40;&#41;;
	sceGuDisplay&#40;GU_TRUE&#41;;
	initialized = 1;
&#125;

void disableGraphics&#40;&#41;
&#123;
	initialized = 0;
&#125;

void guStart&#40;&#41;
&#123;
	sceGuStart&#40;GU_DIRECT, list&#41;;
&#125;

graphics.h

Code: Select all

#ifndef GRAPHICS_H
#define GRAPHICS_H

#include <psptypes.h>

#define	PSP_LINE_SIZE 512
#define SCREEN_WIDTH 480
#define SCREEN_HEIGHT 272

//extern u32* g_vram_base;

#define GFX_TEXT 0
#define GFX_DIRECTX_WIN 1
#define GFX_DIRECTX_ACCEL 2
#define GFX_DIRECTX_SOFT 3

#define DRAW_MODE_COPY_PATTERN 0
#define DRAW_MODE_SOLID 1

#define FONT unsigned char

typedef u32 Color;
#define A&#40;color&#41; &#40;&#40;u8&#41;&#40;color >> 24 & 0xFF&#41;&#41;
#define B&#40;color&#41; &#40;&#40;u8&#41;&#40;color >> 16 & 0xFF&#41;&#41;
#define G&#40;color&#41; &#40;&#40;u8&#41;&#40;color >> 8 & 0xFF&#41;&#41;
#define R&#40;color&#41; &#40;&#40;u8&#41;&#40;color & 0xFF&#41;&#41;
#define makecol&#40;r,g,b&#41; &#40;&#40;r&#41;|&#40;&#40;g&#41;<<8&#41;|&#40;&#40;b&#41;<<16&#41;&#41;

typedef struct RGB
&#123;
  unsigned char r, g, b;
  unsigned char filler;
&#125; RGB;

typedef RGB PALETTE&#91;256&#93;;
//typedef Color PALETTE&#91;256&#93;;

typedef struct
&#123;
  int textureWidth;   // the real width of data, 2^n with n>=0
  int textureHeight;  // the real height of data, 2^n with n>=0
  int imageWidth;     // the image width
  int imageHeight;
  Color* data;
&#125; BITMAP;

typedef struct
&#123;
  Color* pal;
  int nbc;
  int w,h;
  int x,y;
  int chche_idx;
  int cache_hits;
  int size;
  unsigned char* buf;
&#125; RLE_SPRITE;

typedef struct
&#123;
  int w,h,x,y;
&#125; RECT;

//#define PALETTE unsigned char

extern BITMAP* screen;
//extern PALLETE black_palette;
extern FONT* font;

extern BITMAP* load_pcx&#40;const char* filename,PALETTE pal&#41;;
extern void blitImageToImage&#40;int sx, int sy, int width, int height, BITMAP* source, int dx, int dy, BITMAP* destination&#41;;
extern void blitImageToScreen&#40;int sx, int sy, int width, int height, BITMAP* source, int dx, int dy&#41;;
extern void blit&#40;BITMAP *source, BITMAP *dest, int source_x, int source_y, int dest_x, int dest_y, int width, int height&#41;;
extern void blitAlphaImageToImage&#40;int sx, int sy, int width, int height, BITMAP* source, int dx, int dy, BITMAP* destination&#41;;
extern void blitAlphaImageToScreen&#40;int sx, int sy, int width, int height, BITMAP* source, int dx, int dy&#41;;
extern BITMAP* create_bitmap&#40;int width, int height&#41;;
extern void destroy_bitmap&#40;BITMAP* image&#41;;
extern void clearImage&#40;Color color, BITMAP* image&#41;;
extern void clearScreen&#40;Color color&#41;;
extern void clear_to_color&#40;BITMAP *bitmap,Color color&#41;;
extern void fillImageRect&#40;Color color, int x0, int y0, int width, int height, BITMAP* image&#41;;
extern void fillScreenRect&#40;Color color, int x0, int y0, int width, int height&#41;;
extern void rectfill&#40;BITMAP *bmp,int x1, int y1, int x2, int y2, Color color&#41;;
extern void putPixelScreen&#40;Color color, int x, int y&#41;;
extern void putPixelImage&#40;Color color, int x, int y, BITMAP* image&#41;;
extern void putpixel&#40;BITMAP *bmp, int x, int y, Color color&#41;;
extern Color getPixelScreen&#40;int x, int y&#41;;
extern Color getPixelImage&#40;int x, int y, BITMAP* image&#41;;
extern void getpixel&#40;BITMAP *bmp, int x, int y&#41;;
extern void printTextScreen&#40;int x, int y, const char* text, u32 color&#41;;
extern void printTextImage&#40;int x, int y, const char* text, u32 color, BITMAP* image&#41;;
extern void saveImage&#40;const char* filename, Color* data, int width, int height, int lineSize, int saveAlpha&#41;;
//extern int save_bitmap&#40;const char *filename, BITMAP *bmp,const RGB *pal&#41;;
extern void flipScreen&#40;&#41;;
extern void initGraphics&#40;&#41;;
extern void disableGraphics&#40;&#41;;
void drawLineScreen&#40;int x0, int y0, int x1, int y1, Color color&#41;;
extern void drawLineImage&#40;int x0, int y0, int x1, int y1, Color color, BITMAP* image&#41;;
extern void line&#40;BITMAP *bmp, int x1, int y1, int x2, int y2, Color color&#41;;
extern Color* getVramDrawBuffer&#40;&#41;;
extern Color* getVramDisplayBuffer&#40;&#41;;

extern void guStart&#40;&#41;;

#endif

//BITMAP *screen;
//screen->imageWidth=SCREEN_W;
//screen->imageHeight=SCEEN_H;
//screen->textureWidth=1;
//screen->textureHeight=PSP_LINE_SIZE;
//screen->data=getVramDisplayBuffer&#40;&#41;;

//BITMAP *buffer;
//buffer->imageWidth=SCREEN_W;
//buffer->imageHeight=SCEEN_H;
//buffer->textureWidth=1;
//buffer->textureHeight=PSP_LINE_SIZE;
//buffer->data=getVramDrawBuffer&#40;&#41;;

MysticWhiteDragon
Posts: 30
Joined: Thu Feb 16, 2006 8:46 am

Tried something different

Post by MysticWhiteDragon »

I tried pasting some other code in and using Lesson 4's tutorial it says "Image load failed!". Can anyone help me with this problem?

graphics.c

Code: Select all

Image* loadImage&#40;const char* filename&#41;
&#123;
     PCXHEADER pcx;
     int idx = 0;
     int c;
     int i,j;
     int numRepeat;
     FILE *filePtr;
     int width;
     int height;
     unsigned char *pixelData;
     unsigned char *paletteData;
     unsigned char *unscaledData;
     unsigned char *pixel = NULL;
     unsigned char *palette = NULL;

     Image* image = &#40;Image*&#41; malloc&#40;sizeof&#40;Image&#41;&#41;;
     
     filePtr = fopen&#40;filename, "rb"&#41;;
     if &#40;filePtr == NULL&#41;
          return NULL;

     c = getc&#40;filePtr&#41;;
     if &#40;c != 10&#41;
     &#123;
          fclose&#40;filePtr&#41;;
          return NULL;
     &#125;

     c = getc&#40;filePtr&#41;;
     if &#40;c != 5&#41;
     &#123;
          fclose&#40;filePtr&#41;;
          return NULL;
     &#125;

     rewind&#40;filePtr&#41;;

     fgetc&#40;filePtr&#41;;
     fgetc&#40;filePtr&#41;;
     fgetc&#40;filePtr&#41;;
     fgetc&#40;filePtr&#41;;

     pcx.xMin = fgetc&#40;filePtr&#41;;
     pcx.xMin |= fgetc&#40;filePtr&#41; << 8;

     pcx.yMin = fgetc&#40;filePtr&#41;;
     pcx.yMin |= fgetc&#40;filePtr&#41; << 8;

     pcx.xMax = fgetc&#40;filePtr&#41;;
     pcx.xMax |= fgetc&#40;filePtr&#41; << 8;

     pcx.yMax = fgetc&#40;filePtr&#41;;
     pcx.yMax |= fgetc&#40;filePtr&#41; << 8;

     width = pcx.xMax - pcx.xMin + 1;
     height = pcx.yMax - pcx.yMin + 1;
     image->imageWidth = width;
	 image->imageHeight = height;
	 image->textureWidth = getNextPower2&#40;width&#41;;
	 image->textureHeight = getNextPower2&#40;height&#41;;

     pixelData = &#40;unsigned char*&#41;malloc&#40;width*height&#41;;

     fseek&#40;filePtr, 128, SEEK_SET&#41;;
     
     while &#40;idx < &#40;width*height&#41;&#41;
     &#123;
          c = getc&#40;filePtr&#41;;
          if &#40;c > 0xbf&#41;
          &#123;
               numRepeat = 0x3f & c;
               c = getc&#40;filePtr&#41;;

               for &#40;i = 0; i < numRepeat; i++&#41;
               &#123;
                    pixelData&#91;idx++&#93; = c;
               &#125;
          &#125;
          else
               pixelData&#91;idx++&#93; = c;

          fflush&#40;stdout&#41;;
     &#125;

     paletteData = &#40;unsigned char*&#41;malloc&#40;768&#41;;

     fseek&#40;filePtr, -769, SEEK_END&#41;;

     c = getc&#40;filePtr&#41;;
     if &#40;c != 12&#41;
     &#123;
          fclose&#40;filePtr&#41;;
          return NULL;
     &#125;

     for &#40;i = 0; i < 768; i++&#41;
     &#123;
          c = getc&#40;filePtr&#41;;
          paletteData&#91;i&#93; = c;
     &#125;

     if &#40;pixel != NULL&#41;
     &#123;
          free&#40;pixel&#41;;
          pixel = NULL;
     &#125;
     
     image->data = &#40;Color*&#41; memalign&#40;16, image->textureWidth * image->textureHeight * sizeof&#40;Color&#41;&#41;;

     fclose&#40;filePtr&#41;;
     pcx.palette = paletteData;

     unscaledData = &#40;unsigned char*&#41;malloc&#40;width*height*4&#41;;

     for &#40;j = 0; j < height; j++&#41; 
     &#123;
          for &#40;i = 0; i < width; i++&#41; 
          &#123;
               unscaledData&#91;4*&#40;j*width+i&#41;+0&#93; = &#40;unsigned char&#41;palette&#91;3*pixel&#91;j*width+i&#93;+0&#93;;
               unscaledData&#91;4*&#40;j*width+i&#41;+1&#93; = &#40;unsigned char&#41;palette&#91;3*pixel&#91;j*width+i&#93;+1&#93;;
               unscaledData&#91;4*&#40;j*width+i&#41;+2&#93; = &#40;unsigned char&#41;palette&#91;3*pixel&#91;j*width+i&#93;+2&#93;;
               unscaledData&#91;4*&#40;j*width+i&#41;+3&#93; = &#40;unsigned char&#41;255;
          &#125;
     &#125;

	 free&#40;unscaledData&#41;;
     return image;
&#125;

graphics.h

Code: Select all

typedef struct
&#123;
  unsigned char manufacturer;
  unsigned char version;
  unsigned char encoding;
  unsigned char bits;
  unsigned char xMin;
  unsigned char yMin;
  unsigned char xMax;
  unsigned char yMax;
  unsigned char *palette;
&#125; PCXHEADER;
Post Reply