News:

Printed Amstrad Addict magazine announced, check it out here!

Main Menu

CPCTelera 1.4/ SDCC structs and initilization

Started by homey, 21:10, 03 July 17

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

homey

Hello everybody,


I try to play around with CPCTelera for my favourite home computer  :)
I think CPCTelera itslef is not my current problem, but it is a about the sdcc compiler.
My Project was created by the mkproject tool from CPCTelera and I added a c header file with a struct definition.




#ifndef MAPITEM_H
#define MAPITEM_H




typedef struct Map
{
    unsigned char width;
    unsigned char heigth;
    unsigned char amountOfFields;
   
   
}MapItem;


void MapItem_drawMapMasked(MapItem* map);


#endif /* MAPITEM_H */





On my main.c file I include the header and try to initialize a varible of the struct and give it to a function in another file.


Here my main file:

#include "GameItems/MapItem.h"


void main(void)
{
    MapItem mp = {4, 4, 4};
    MapItem_drawMapMasked(&mp);
}



The draw function is implemented that way




void MapItem_drawMapMasked(MapItem* map)
{
    unsigned char counter = 0;   
    for(counter = 0; counter < map->amountOfFields; counter++)
    {
        MapItem_drawField(counter, map);
    }
}



What currently happens is that the for loop of the draw function behaves like map->amountOfFields qas initilized with 1. What means 1 Field will be drawn to video memory. I checked that my video address calculations are correct and changed the for loop to





void MapItem_drawMapMasked(MapItem* map)
{
    unsigned char counter = 0;   
    for(counter = 0; counter < 4; counter++)
    {
        MapItem_drawField(counter, map);
    }
}



This works as aspected. Then  I changed my main file to




void main(void) {
   
    //map->fields = fied
    //map.fields = &fields[0];
   
    //mp->amountOfFields = 15;
    unsigned char counter;
    MapItem mp = {4, 4, 4};
 
    for(counter = 0; counter < mp.amountOfFields; counter++)
    {
        MapItem_drawField(counter, &mp);
    }
    //MapItem_drawMapMasked(&mp);
   


}



Here mp.amountOfFields is 4 and works also as aspected.


Have anyone an idea why is something going wrong if i pass the pointer to the MapDrawing function as a pointer ?


Thanks in advance


homey

Arnaud

#1
Hello, and welcome @homey.

I see the problem you have to disable the firmware with cpct_disableFirmware().
I don't know exactly why it don't works, but i guess the structure MapItem mp is in the same memory area of the CPC firmware.

#include <cpctelera.h>
#include <stdio.h>

typedef struct
{
    unsigned char width;
    unsigned char heigth;
    unsigned char amountOfFields;
}MapItem;

void MapItem_drawMapMasked(MapItem *mp)
{
    unsigned char counter = 0;   
    for(counter = 0; counter < mp->amountOfFields; counter++)
    {
        cpct_drawStringM1("A", (u8*)0xC000 + counter*2, 1, 0);
    }
}

void main(void)
{
    const MapItem mp = { 4, 4, 10 };
    cpct_disableFirmware();
    MapItem_drawMapMasked(&mp);
   
    while(1);
}

SRS

Did you try to compile that with z88dk, too ?


Or maybe you have to set a data-location above the firmware area, i.e. at &8000, if you want to use firmware in your game.

This could be done iirc changing "--data-loc 0" within build_config.mk of your project.

ronaldo

It seems that it could be a translation bug from SDCC compiler. The versión comming with CPCtelera 1.4 has some small bugs that sometimes affect this kind of code.

To confirm this hypothesis, I would need your complete project, to try and compile it and see what actually happens. This quetions are normally solved by debugging with WinAPE and checking what it is actually doing.

ronaldo

@homey I have tested your code without the drawing function and it works as expected. Debugging your code is required to see what happens. After the test, I think that this might be a lateral effect of a bug in your drawing function. Drawing is copying data to the video memory. If something fails, you may end up copying it to a different place in memory and, sometimes, overwritting variable values. If you change your code, variables may end up in a different place in memory, and then the overwritting effect may apparently disappear.

I think this is what is happening to you. Your structure values get overwritten by a bug in your drawing code. If you change your code, new placements in memory prevent that from happening. You definitely need to debug the code and monitor changes in your variables to see when do they happen and who is responsible for it.

homey

Thanks for your advice. I will check them today.

Powered by SMFPacks Menu Editor Mod