News:

Printed Amstrad Addict magazine announced, check it out here!

Main Menu
avatar_EgoTrip

[cpctelera] Displaying Contents of a Variable

Started by EgoTrip, 18:02, 24 October 15

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

EgoTrip

How do I display the contents of a variable in cpctelera? I'm probably missing something really obvious here as usual but I can't seem to figure it out.

arnoldemu

Quote from: EgoTrip on 18:02, 24 October 15
How do I display the contents of a variable in cpctelera? I'm probably missing something really obvious here as usual but I can't seem to figure it out.
displaying it on the screen?
and in decimal?


EgoTrip

Yes, I want to display health and score which are held in u8 and u16 respectively

arnoldemu

If there is no function to do this already you can convert the number to a string and print it with the normal string functions in cpctelera.
To do this convert to hundreds, tens, units then put each digit as a character into a string and display the string.


char str[4]; /* temp string storage for 3 digits plus string terminator char */
int value, hundreds, tens, units;

value = health;
hundreds = health/100;
value = value-(hundreds*100);
tens = value/10;
value = value-(tens*10);
units = value;

str[3] = '\0';
str[0] = hundreds+'0';
str[1] = tens+'0';
str[2] = units+'0';


For larger numbers use 1000's etc.

But if there is an actual number printing routine use that.


Arnaud

Here the code i use to spy my variables, you can display integer, hexa and string with cpctelera.
You can easily modify it in order to use the drawing function of cpctelera.

Hope it help.

EgoTrip

Thanks for the routines, will let you know how I get on.

FloppySoftware

Quote from: EgoTrip on 18:02, 24 October 15
How do I display the contents of a variable in cpctelera? I'm probably missing something really obvious here as usual but I can't seem to figure it out.

May be you could use the sprintf() function from the C stdio library to convert the variable value to its string representation. Then, you could use a cpctelera function to print it on the screen.

floppysoftware.es < NEW URL!!!
cpm-connections.blogspot.com.es

EgoTrip

Thanks for the help. That routine in the zip uses firmware and I have it disabled.

I just get loads of gibberish on the screen (and a load of warnings in the compile window) when I try to display using Arnoldemu's routine. But instead I decided to split the variable into seperate variables instead of strings and add 48 to them, then use cpct_drawCharM1 to display them and it seems to be working now.

ronaldo

@EgoTrip: If you only want to display some value for debugging purposes a simple way to do it is using sprintf()

#include <stdio.h>
...
{
   u8 str[6];   // string to write the value on
   u8 points=100;
   sprintf(str, "%d", points);
   cpct_drawStringM1(str, pvideomem, 1, 0);
}
...

If you are implementing a scoreboard or something similar, you have to take into account how often does it refresh. If you are planning to refresh it every frame or every 2 frames, using cpct_drawString or cpct_drawChar is not recommended. These functions read characters from ROM and have to interpret their codification and transform them into screen pixel format. That takes time. Even them being extremelly fast compared to firmware routines, they will always be very slow compared to a drawSprite routine that only has to copy bytes.

So, if you are implementing a scoreboard that refreshes often, it's interesting to think of having 10 sprites with your numbers and use cpct_drawSprite or, even better, cpct_drawTileAligned2x8. You may use a mixture of both, using drawString functions for text that does not change and drawSprite/drawTile functions for numbers that change often.

Then, calculations for splitting into digints (like you are doing now until adding 48) are also necessary.

EgoTrip

#9
Thanks for the help.

I have it set so it only updates the HUD when something happens, ie a jewel is collected or you are hurt. It doesn't do it every frame. It slowed the game to a crawl when it was running every frame.

I will consider using sprites for the digits, however I am also using text messages throughout the game. While I'm on this subject how do I add my own custom font?

ronaldo

Right now, there is no low-level support for custom fonts on CPCtelera. You'll have to create your own printing functions using sprites for your font. This is one of the items on the task list for future versions. I'd like to add support for custom fonts either based on sprites or on pixel definitions (like the ones in ROM). But that will be for next iterations, when time comes back to my backpack :D.

Arnaud

Everyday a new feature to add to cpctelera  ;D

lachlank

Quote from: EgoTrip on 20:30, 24 October 15

I will consider using sprites for the digits, however I am also using text messages throughout the game. While I'm on this subject how do I add my own custom font?


I had a couple of examples of printing strings using sprites and sample fonts in the main CPCTelera thread.


#CPCtelera 1.0. Amstrad CPC game development library official release
#CPCtelera 1.0. Amstrad CPC game development library official release


Powered by SMFPacks Menu Editor Mod