Converting and displaying an image on the screen (C with SDCC)

Pincha aquí para verlo en español

In this tutorial we will see how to convert and display an image on the screen as simple as possible. To avoid having to load files on the Amstrad CPC, we will embed the image in binary executable itself.

To begin we will find the image you want to convert, in my case I will use one of my cousin (do not ask me the phone):

rihanna

To convert it we will use the great tool ConvImgCPC. I've configured as shown in the image below to get the result I liked:

rihanna

Once we have the result that we like, we will export the image to use in our program, for this select the "asm mode" and click on "Save Picture" and save as rihanna.asm (in this case) to the palette we keep the colors that appear at the bottom right (where it says "16 Most used colors").

As the output format of the image in asm is not directly compatible with C, we will adapt very easily, open the file asm (rihanna.asm) in a text editor (Notepad, UltraEdit, etc. ..) and look for the option to "Replace" (in notepad CTRL-R) and we say the program that replace the character '#' with '0x' and click "Replace All", then we replace the text 'DB' with ', ' and click "Replace All". We have almost adapted the file, you must delete the first ',' and to name the array (const char RihannaScreen) is very important to put the word 'const' because if we do not, SDCC puts data load instructions (LD). Finally add the palette, keys and record the file as being rihanna.h as follows:

/*
;Généré par ConvImgCpc Version 0.16
; Mode 0
; 80x200
*/
const unsigned char RihannaPalette[16] = {26,0,13,16,25,12,3,17,4,15,7,1,22,23,14,9};

const char RihannaScreen[] = 
{
               0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x51
        ,      0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0
        ,      0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0
        ,      0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0
        ,      0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0
        ,      0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xCA, 0xC0, 0xC0
        ,      0xC0, 0xC0, 0xC0, 0xC0, 0x48, 0xC0, 0xC0, 0xC0
        ,      0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0x06, 0xE0
        ,      0xC0, 0xC0, 0xC0, 0xC0, 0x00, 0x00, 0x00, 0x00
        ,      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
        ,      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
        ,      0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0
        ,      0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0
        ,      0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0
        ,      0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0
        ,      0xC0, 0xC0, 0xC0, 0xC0, 0xDA, 0xC0, 0xC0, 0xC0
        ,      0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0
        ,      0xC0, 0xC0, 0x48, 0xC5, 0xE0, 0x18, 0x1C, 0xC0
        ,      0xC0, 0xC0, 0xD0, 0x1C, 0x00, 0x00, 0x00, 0x00
        ,      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
        ,      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
        ,      0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0
        ,      0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0
        ,      0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0
        ,      0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0
        ,      0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0
        ,      0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0
        ,      0xC0, 0x84, 0x48, 0x84, 0x20, 0x48, 0xC0, 0xC0
        ,      0x48, 0x94, 0x98, 0x4C, 0x00, 0x00, 0x00, 0x00
        ,      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
        ,      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
        ,      0x3F, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0
        ,      0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0
        ,      0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0
        ,      0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0
        ,      0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0
        ,      0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0x84
        ,      0xC0, 0x1C, 0xCC, 0x4C, 0xE0, 0xC0, 0xC0, 0x84 

Lots of lines...

        ,      0x78, 0xF0, 0x49, 0x4C, 0xCC, 0x98, 0x98, 0x30
        ,      0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
        ,      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
        ,      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

 

To display it, we make a simple program, the only thing that does is put mode 0, put a black palette, copy the image to the video memory and finally put the actual image palette, the program would look like this:

////////////////////////////////////////////////////////////////////////
// Image01.c
// Show a image
// Mochilote - www.cpcmania.com
////////////////////////////////////////////////////////////////////////
#include <string.h>

#include "rihanna.h"

#define NUM_COLORS 16

const unsigned char BlackPalette[NUM_COLORS] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

void SetColor(unsigned char nColorIndex, unsigned char nPaletteIndex)
{
  __asm
    ld a, 4 (ix)
    ld b, 5 (ix)
    ld c, b
    call #0xBC32 ;SCR SET INK
    __endasm;
}

void SetPalette(const unsigned char *pPalette)
{
  unsigned char nColor = 0;

  for(nColor = 0; nColor < NUM_COLORS; nColor++)
    SetColor(nColor, pPalette[nColor]);
}

void main()
{
  //SCR_SET_MODE 0
  __asm
    ld a, #0
    call #0xBC0E
  __endasm;

  //SCR SET BORDER 0
  __asm
    ld b, #0 ;black
    ld c, b
    call #0xBC38
  __endasm;

  SetPalette(BlackPalette);

  memcpy(0xC000, RihannaScreen, 0x3FFF);

  SetPalette(RihannaPalette);

  while(1) {};
}
////////////////////////////////////////////////////////////////////////

If you load an emulator would see the following:

animation

You could download a zip with all files (source code, bat to compile, binary and dsk's) here: Converting_and_displaying_an_image_on_the_screen.zip

 

www.CPCMania.com 2012