Hi everyone! As you may know, I have been delving into C for the CE and for the PC/Arduino, but I've been having some questions. So instead of asking on IRC, I decided to create a topic so I have something to go back to and I don't have to ask the same question over and over again when I forget something Smile
I've also decided to post about the programs I've made just experimenting here. If I start an actual one, I will make a topic

How to set up Debug Mode for CEmu
    1) Enable it in the makefile DEBUGMODE ?= DEBUG
    2) Add #include <debug.h> to the source files
    3) Use this code for variables. dbg_sprintf(dbgout, "%d\n", variableName);
    4) Use this code for text. dbg_sprintf(dbgout, "Hi\n");

How to Store and Retrieve Highscores or Variables from an AppVar
    1) Declare file as a ti_var_t variable, and highscore as an uint8_t
    2) When you want to store a value to the appvar, use this code. Interchange highscore with the name of your variable getting stored, and scorevar with the name of your appvar.
    Code:
    ti_CloseAll();
        file = ti_Open("scorevar", "w");
        if (file)
             ti_Write(&highscore, sizeof(highscore), 1, file); 
       }
    3) When you want to store a value from the appvar to a variable, use this code. Interchange highscore with the name of your variable getting retrieved, and scorevar with the name of your appvar.
    Code:
    ti_CloseAll();
       file = ti_Open("scorevar", "r");
       if (file) {
          ti_Read(&highscore, sizeof(highscore), 1, file);
         }



Thanks in advance Smile
1) Change DEBUGMODE ?= NDEBUG to DEBUGMODE ?= DEBUG in the makefile

2) Add #include <debug.h> to your source files

3) Use the following functions:


Code:
dbg_sprintf(dbgout,"CEmu Debug Test: %d\n", 16);
assert(CONDITION);
debugger();
abort();
See here for printf formatting reference.
MateoConLechuga wrote:
1) Change DEBUGMODE ?= NDEBUG to DEBUGMODE ?= DEBUG in the makefile

2) Add #include <debug.h> to your source files

3) Use the following functions:


Code:
dbg_sprintf(dbgout,"CEmu Debug Test: %d\n", 16);
assert(CONDITION);
debugger();
abort();

Ok...
So what do I do to initiate dbgout? Do I just put dbg_sprintf() whenever I want, or is there something else I need to do?
No, everything is handled for you. Just:


Code:
dbg_sprintf(dbgout,"my string\n");
Ok, so to print variable y and string "HI" and go to a new line do this:


Code:

dbg_sprintf(dbgout, "Hi/n", y);
dbg_sprintf(dbgout, "%d/n", y);


Correct?
Merth just linked the printf guide above and I just demonstrated an example. Please look and see what you are doing wrong, and then ask the question Smile
Ok, so is this valid code to save a variable called highscore to an AppVar high?


Code:

/* CE Keypad C Library */
#include <fileioc.h>

/* Main Function */
void main(void) {
    unsigned int file;
    int highscore;
    /* Close all open file handles before we try any funny business */
    ti_CloseAll();
     
    /* Open a file for writting. */
    file = ti_Open("high", "a");
     
    /* write some things to the file */
    if (file) {
         ti_Write(highscore, sizeof(3), sizeof(highscore)/sizeof(3), file);
    } 
}


And is this valid code to read the variable highscore from the appvar, then store highscore to highscore in the program? (everything is already declared above)


Code:

   ti_CloseAll();
   file = ti_Open("swipehigh", "a");
   if (file) {
         ti_Read(highscore, sizeof(3), sizeof(highscore)/sizeof(3), file);
    }   
Quickly spotting some issues:
Code:
/**
 * Writes to the current variable pointer given:
 * data:
 *  pointer to data to write
 * size:
 *  size (in bytes) of the data we are writting
 * count:
 *  number of data chunks to write to the variable slot
 * slot:
 *  varaible slot to write the data to
 * Returns the number of chunks written (should be equal to count)
 */
size_t ti_Write(const void *data, size_t size, size_t count, const ti_var_t slot);

You use ti_Write with data as type int. That wouldn't grab the value of highscore, rather interpret it as a memory location to read from (highscore ==3, then it reads data from 0x000003). Rather:

Code:
ti_Write(&highscore, sizeof(highscore), 1, file);

The sizeof(3) is interpreted as the sizeof((int)3) -> sizeof(int) -> 3.

I have count as 1 to explicitly mean to read only 1 value from the pointer.
________________________

In ti_Open, the const char* mode variable has the following docs for a value of "a":

Code:
 * "a"  - Appends to a file. Writing operations, append data at the end of the file. The file is created if it does not exist.  (RAM)


If you rerun this program, you will be appending new data to the end, however you read from the start of the file. Probably want "w":

Code:
 * "w"  - Creates an empty file for writing. Overwrites file if already exists.                                                 (RAM)

________________________

The filenames that you have in the writing and reading section don't match, you won't be using the same file.
________________________

ti_Read has a similar declaration to ti_Write, so &highscore will be needed to tell the function where to write data to (in this case, into the highscore variable).
I think you want
Code:
ti_Write(&highscore, sizeof(highscore), 1, file);
and
Code:
ti_Read(&highscore, sizeof(highscore), 1, file);


Because the first argument is the address of what you want to write, the second argument is its size, the third is the number of them you want to write, and the last is the file.

Also, if you look at the prototype of ti_Open, it returns a ti_var_t, so you should change the type of file to be

Code:
ti_var_t file;
So I've started documenting the answers to all of my questions in the OP. If anyone asks questions about such things, I believe this will help.
So, more questions! How might I go about using the Tilemap functions in graphx.h?

I realize there is this command:

Code:

void gfx_Tilemap(gfx_tilemap_t *tilemap, uint24_t x_offset, uint24_t y_offset);

But what about this info? How would I set this data up?

Code:

/* Type for tilemap */
typedef struct gfx_tilemap {
   uint8_t *map;             /* pointer to indexed map array */
   gfx_image_t **tiles;          /* pointer to tiles */
   uint8_t tile_height;      /* individual tile height */
   uint8_t tile_width;       /* individual tile width */
   uint8_t draw_height;      /* number of rows to draw in the tilemap */
   uint8_t draw_width;       /* number of cols to draw tilemap */
   uint8_t type_width;       /* 2^type_width = tile_width */
   uint8_t type_height;      /* 2^type_height = tile_height */
   uint8_t height;           /* total number of rows in the tilemap */
   uint8_t width;            /* total number of cols in the tilemap */
   uint8_t y_loc;            /* y pixel location to begin drawing at */
   uint24_t x_loc;           /* x pixel location to begin drawing at */
} gfx_tilemap_t;

typedef enum gfx_tilemap_type {
   gfx_tile_2_pixel = 1,      /* Set when using 2 pixel tiles */
   gfx_tile_4_pixel,          /* Set when using 4 pixel tiles */
   gfx_tile_8_pixel,          /* Set when using 8 pixel tiles */
   gfx_tile_16_pixel,         /* Set when using 16 pixel tiles */
   gfx_tile_32_pixel,         /* Set when using 32 pixel tiles */
   gfx_tile_64_pixel,         /* Set when using 64 pixel tiles */
   gfx_tile_128_pixel,        /* Set when using 128 pixel tiles */
} gfx_tilemap_type_t;


And the final question: How do I load a map using it? Do I need different sprites that are how ever large my tiles are and then I call some sort of command that sets them to certain places on the tilemap for it to draw?
And I'm assuming I really should learn how to use buffers to erase things.

(I'm thinking about making some type of platformer)
To draw to the buffer, you use gfx_SetDraw(gfx_buffer); or gfx_SetDrawBuffer();. Then when you want to set the command gfx_SwapDraw(); where you want to put all of what you drew to the screen. I recommend not doing that more than once in a loop., and if it depends on the press of a key, then use an if(key) {}. Put everything that you output in there and keep your input inside the regular loop.
Thanks, I think that should help me...

So about the above question: Adriweb was kind enough to remind me that there are example for a reason, and one of them is about tilemaps. Anyways, I looked into it, and the only thing I don't really understand is how the tilemapdata.c is generated. It is located in the main src directory and these are teh contents:


Code:

#include <stdint.h>

/* This is the data that is displayed with the tilemap */
uint8_t tilemap_map[] = {
   0x20,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x22,0x14,0x15,0x04,0x05,0x20,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x22,
   0x30,0x54,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x53,0x32,0x04,0x05,0x14,0x15,0x40,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x53,0x32,
   0x30,0x32,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x30,0x32,0x14,0x15,0x04,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x32,
   0x30,0x32,0x01,0x10,0x10,0x10,0x00,0x00,0x01,0x00,0x01,0x00,0x30,0x32,0x04,0x05,0x14,0x15,0x00,0x50,0x51,0x51,0x52,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x32,
   0x30,0x32,0x00,0x10,0x10,0x10,0x00,0x01,0x00,0x01,0x00,0x01,0x30,0x32,0x14,0x15,0x04,0x05,0x00,0x60,0x61,0x61,0x62,0x00,0x00,0x00,0x10,0x10,0x10,0x00,0x30,0x32,
   0x30,0x32,0x01,0x10,0x10,0x10,0x00,0x00,0x01,0x00,0x01,0x00,0x40,0x42,0x04,0x05,0x14,0x15,0x00,0x60,0x61,0x61,0x62,0x00,0x00,0x00,0x10,0x10,0x10,0x00,0x30,0x32,
   0x30,0x32,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x00,0x14,0x15,0x04,0x05,0x00,0x70,0x71,0x71,0x72,0x00,0x00,0x00,0x10,0x10,0x10,0x00,0x30,0x32,
   0x30,0x32,0x01,0x00,0x01,0x00,0x00,0x10,0x10,0x10,0x10,0x10,0x01,0x00,0x04,0x05,0x14,0x15,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x32,
   0x30,0x32,0x00,0x01,0x00,0x00,0x00,0x10,0x10,0x10,0x10,0x10,0x00,0x00,0x14,0x15,0x04,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x32,
   0x30,0x32,0x01,0x00,0x00,0x00,0x01,0x10,0x10,0x10,0x10,0x10,0x01,0x00,0x04,0x05,0x14,0x15,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x32,
   0x30,0x32,0x00,0x01,0x00,0x01,0x00,0x10,0x10,0x10,0x10,0x10,0x00,0x00,0x14,0x15,0x04,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x32,
   0x30,0x32,0x01,0x00,0x01,0x00,0x01,0x10,0x10,0x10,0x10,0x10,0x01,0x00,0x04,0x05,0x14,0x15,0x00,0x00,0x00,0x20,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x43,0x32,
   0x30,0x32,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x00,0x14,0x15,0x04,0x05,0x00,0x00,0x00,0x40,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x53,0x32,
   0x30,0x32,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x20,0x22,0x04,0x05,0x14,0x15,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x32,
   0x30,0x32,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x30,0x32,0x14,0x15,0x04,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x32,
   0x30,0x32,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x30,0x32,0x04,0x05,0x14,0x15,0x00,0x00,0x00,0x00,0x00,0x10,0x10,0x10,0x10,0x10,0x10,0x00,0x30,0x32,
   0x30,0x32,0x00,0x00,0x00,0x01,0x00,0x01,0x10,0x10,0x00,0x01,0x30,0x32,0x14,0x15,0x04,0x05,0x00,0x00,0x00,0x00,0x00,0x10,0x10,0x10,0x10,0x10,0x10,0x00,0x30,0x32,
   0x30,0x32,0x01,0x00,0x01,0x00,0x01,0x00,0x10,0x10,0x01,0x00,0x30,0x32,0x04,0x05,0x14,0x15,0x00,0x00,0x00,0x00,0x00,0x10,0x10,0x10,0x10,0x10,0x10,0x00,0x30,0x32,
   0x30,0x32,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x30,0x32,0x14,0x15,0x04,0x05,0x00,0x00,0x00,0x00,0x00,0x10,0x10,0x10,0x10,0x10,0x10,0x00,0x30,0x32,
   0x30,0x32,0x01,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x30,0x32,0x04,0x05,0x14,0x15,0x00,0x00,0x00,0x00,0x00,0x10,0x10,0x10,0x10,0x10,0x10,0x00,0x30,0x32,
   0x30,0x32,0x00,0x10,0x10,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x30,0x32,0x14,0x15,0x04,0x05,0x00,0x00,0x00,0x00,0x00,0x10,0x10,0x10,0x10,0x10,0x10,0x00,0x30,0x32,
   0x30,0x32,0x01,0x10,0x10,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x30,0x32,0x04,0x05,0x14,0x15,0x00,0x00,0x00,0x00,0x00,0x10,0x10,0x10,0x10,0x10,0x10,0x00,0x30,0x32,
   0x30,0x32,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x00,0x30,0x32,0x14,0x15,0x24,0x25,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x32,
   0x30,0x44,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x43,0x32,0x04,0x05,0x20,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x43,0x32,
   0x40,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x42,0x14,0x15,0x40,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x42,
};


I hope I am correct in believing that this tells the commands where to put each tile.

Thanks in advance!
It was generated manually. Each one represents the index in the tile array, which makes it easy to do animations across multiple items simply by changing the tile's data pointer. Hope this helps! Smile
OK then... So how would I go about displaying the tilemap? Would I have to generate that thing to do it?

If so, how would l create it?

Also, how would I decompress my sprites? I was looking at an example and it doesn't seem to make much sense.
So the tilemap is built using the structure, which I think you figured out. You can then create the tilemap manually using indexes, or you can write a program to do it for you. Decompression doesn't make any sense right now for tilemaps, so I would advise against it until I can make it easier and better Smile
MateoConLechuga wrote:
Decompression doesn't make any sense right now for tilemaps, so I would advise against it until I can make it easier and better Smile


I don't really want to use it for a tilemap anyway, its more for large sprites that I want to use.
So, if I am going to create a tilemap using an editor, what format should I save it in? Base64, CSV, XML? Those are just formats that I saw Tiled had as options.
Unicorn wrote:
So, if I am going to create a tilemap using an editor, what format should I save it in? Base64, CSV, XML? Those are just formats that I saw Tiled had as options.


You want this to fit on a CE, right? You have some options:

  1. Export as CSV, open that with Notepad++, and use find+replace to turn the CSV into a 2D int array.
  2. Make a Python export script to generate the right int array you need.
  3. Export as Base64 and parse it (see TMX docs) along with whatever compression it comes with. (Not recommended.)
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement