Author Topic: Ashbad's C help  (Read 3766 times)

0 Members and 1 Guest are viewing this topic.

Ashbad

  • Guest
Ashbad's C help
« on: June 09, 2011, 03:26:15 pm »
I have a few questions, more focused on C itself rather than just Prizm C in general, and all of these assume I'm using C99:


1. how can structs be treated, when used with with a typedef declaration?  Can you do things like make an array of structs in typedef form, or would you have to do something like make a struct of structs?  Would this work?

Code: [Select]
typedef struct {
  int health;
  int magic;
} enemy;

enemy[] Bestiary = {enemy = {2,4}, enemy = {3,6}}


2. how much can I butcher the #define system?  Would this setup be possible?

Code: [Select]
#define byte[] unsigned char[]
#define new_string(name,value) byte[] name = value

I made this for myself for easy string declaration, and it obviously just would turn something like "new_string(NAME, "Lekens");" to "unsigned char[] NAME = "Lekens";"   would it work?


3. within a struct declaration, can you access "members" of the struct inside of it, in a way someway like this.member?  While I truly doubt it would work like this, is there a form of it that would work like this?

Code: [Select]
typedef struct {
  int health;
  int health = this.health;
} enemy;

while it's not the greatest example, I think it shows what I mean :P

help?  tia.
« Last Edit: June 09, 2011, 03:28:28 pm by Ashbad »

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Ashbad's C help
« Reply #1 on: June 09, 2011, 03:31:47 pm »
To access members inside a code, try this:

Code: [Select]
struct point {
   int x;
   int y;
} my_point;
 
struct point *p = &my_point;  // To declare p as a pointer of type struct point
 
(*p).x = 8;                   // To access the first member of the struct
p->x = 8;                     // Another way to access the first member of the struct
« Last Edit: June 09, 2011, 03:32:06 pm by Scout »

Ashbad

  • Guest
Re: Ashbad's C help
« Reply #2 on: June 09, 2011, 03:35:09 pm »
well, yeah, but what I meant is like accessing a member of a struct inside the declaration of the struct.  Like so:

Code: [Select]
struct point {
   int x;
   int y = this.x;
} my_point;

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Ashbad's C help
« Reply #3 on: June 09, 2011, 03:38:31 pm »
Why don't just use the variable name? It will work from within the struct, I'm pretty sure.

So in your above sample:

Code: [Select]
struct point {
   int x;
   int y = x*2;         //I'm pretty sure this will work and "y" will be twice x
} my_point;
« Last Edit: June 09, 2011, 03:41:28 pm by Scout »

Ashbad

  • Guest
Re: Ashbad's C help
« Reply #4 on: June 09, 2011, 03:43:43 pm »
oh, okay, that's perfect :) that answers number 3, number 1 I finally found was true online (I can make an array of structs) and #2 seems okay and was probably stupid to ask about :P

Offline hoffa

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 322
  • Rating: +131/-13
    • View Profile
Re: Ashbad's C help
« Reply #5 on: June 09, 2011, 04:10:16 pm »
Concerning the second question, you could just define a new type with typedef; typedef unsigned char byte. If you want a nice way to store strings, you could statically allocate (i.e. it will be allocated during compilation; you shouldn't be able to modify them during execution) them by doing something like char *s = "I like turtles". If you want to be able to change your strings, just allocate some space with malloc and make a pointer point to the newly allocated space (and then you should be able to pass it around functions and modify the string as you want).
« Last Edit: June 09, 2011, 04:29:30 pm by hoffa »

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Ashbad's C help
« Reply #6 on: June 10, 2011, 04:53:39 am »
oh, okay, that's perfect :) that answers number 3, number 1 I finally found was true online (I can make an array of structs) and #2 seems okay and was probably stupid to ask about :P

Good to know! If you have any more questions, feel free to ask.

Ashbad

  • Guest
Re: Ashbad's C help
« Reply #7 on: June 12, 2011, 07:15:08 pm »
I truly need help, I've been going over this code for a few hours now, and I still don't get what's wrong with it.  Here are my source files:

ROLAT_defs.h
Code: [Select]
/*
All definitions for everything needed for ROL:AT are included here,
sorted by type and meaning
*/


//-----------------------
//   TECHNICAL THINGS
//-----------------------

#define string(name, value) byte name##[] = value

#define clean_exit 0
#define error_exit 1

Main.c
Code: [Select]
#include "ROLAT_defs.h"
#include "battle_system.c"


int main() {
battle_main();
return clean_exit;
}

useful.c
Code: [Select]
#include "keyboard_syscalls.h"
#include "keyboard.hpp"

#define LCD_WIDTH_PX 384
#define LCD_HEIGHT_PX 216
#define MASK_RED 0xf800

void CopySpriteMasked(const char*data, int x, int y, int width, int height, int maskcolor);
int PRGM_GetKey(void);

void CopySpriteMasked(const char*data, int x, int y, int width, int height, int maskcolor) {
   char* VRAM = (char*)0xA8000000;
   VRAM += 2*(LCD_WIDTH_PX*y + x);
   for(int j=y; j<y+height; j++) {
      for(int i=x; i<x+width;  i++) {
         if ((((((int)(*data))&0x000000FF)<<8) | ((((int)(*(data+1))))&0x000000FF)) != maskcolor) {
            *(VRAM++) = *(data++);
            *(VRAM++) = *(data++);
         } else { VRAM += 2; data += 2; }
      }
      VRAM += 2*(LCD_WIDTH_PX-width);
   }
}

int PRGM_GetKey(){
unsigned char buffer[12];
PRGM_GetKey_OS( buffer );
return ( buffer[1] & 0x0F ) * 10 + ( ( buffer[2] & 0xF0 ) >> 4 );
}


Enemies.c
Code: [Select]
const char Nomekati[] = {
   0xf8,0x00,0xf8 ... (you get it, though there's 3 colors: 0xf800 (RED), 0x0000 (white) and 0xFFFF (black)
   //goes on and on, it's a 64x64 image (troll face)
};

battle_system.c
Code: [Select]
#include "bestiary.c"
#include "display_syscalls.h"
#include "ROLAT_defs.h"
#include "useful.c"

#define endof_render_exit 2

int render_status = clean_exit;
int Current_Opponents_nums[] = {1,0,0,0,0,0};
enemy Current_Opponents[6];

int battle_main(void);
int renderloop_bs(void);

int battle_main() {
for (int i=0;i<=5;i++) {
if (Current_Opponents_nums[i] != 0) {
CreateDynamicEnemy(&(Current_Opponents[i]), Current_Opponents_nums[i]);
}
}

while (render_status == clean_exit) {
render_status == renderloop_bs();
if (PRGM_GetKey()) {
render_status = endof_render_exit;
}
}
return (render_status == endof_render_exit) ? clean_exit : error_exit;
}

int renderloop_bs() {
Bdisp_AllCr_VRAM();
CopySpriteMasked(Bestiary[1].sprite_data, 20, 20, Bestiary[1].width, Bestiary[1].height, MASK_RED);
Bdisp_PutDisp_DD();
return clean_exit;
}

bestiary.c
Code: [Select]
#include "ROLAT_defs.h"
#include "stdlib.h"
#include "Enemies.c"
#include "string.h"

typedef struct {
unsigned char name[9];
const void*sprite_data;
unsigned short magic;
unsigned char intelligence, hit, move_1, move_2, move_3;
unsigned short attack, defense;
unsigned char is_boss;
unsigned char can_heal;
unsigned char type;
unsigned char orb_held;
int width, height, gp, xp, hp, max_hp;
} enemy;
 
int ENEMY_SIZE = sizeof(enemy);
void CreateDynamicEnemy(enemy*p, int bestiary_num);

enemy Bestiary[] = {
{"/0",(const char*)0x00000000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{"Nomekati\0",Nomekati,0,0,0,200,15,5,50,50,75,25,30,0,0,0,0,64,64}
};

 
void CreateDynamicEnemy(enemy*p, int bestiary_num) {
memcpy(p,&(Bestiary[bestiary_num]),ENEMY_SIZE);
}



And yes, I do copy "ROLAT_defs.h" to the "include" header of the Prizm SDK, I use this Batch script I made in a few minutes to do that and more:

Code: [Select]
COPY /V /Y F:\ROLAT\Code F:\PrizmSDK\
COPY /V /Y F:\ROLAT\Code\Flibs F:\PrizmSDK\
COPY /V /Y F:\ROLAT\Code\Data F:\PrizmSDK\
COPY /V /Y F:\ROLAT\Code\Headers F:\PrizmSDK\include
COPY /V /Y F:\ROLAT\Graphics\Icons F:\PrizmSDK\icons

CD "F:\PrizmSDK"
DEL ROLAT.bin
DEL Main.bin
bin\make.exe %*

@ECHO off

IF NOT EXIST F:\PrizmSDK\ROLAT.g3a GOTO END

MOVE /Y F:\PrizmSDK\ROLAT.g3a F:\ROLAT\Builds\PlaceH.placeh
CD F:\ROLAT\Builds\
ECHO PlaceHold > ROLAT.g3a
COPY /Y PlaceH.placeh ROLAT.g3a
SET FileDate=%date:/=%_%time::=%
REN ROLAT.g3a "ROLAT_%FileDate%.g3a"
ECHO Build Version ROLAT_%FileDate%

:END
PAUSE
@ECHO on

What the problem is isn't that there's a problem getting things to compile alright -- I already fixed all the files so that it compiles with no errors whatsoever.  The real problem is when I run the Add-in, I get this error:

Code: [Select]
System ERROR
 REBOOT    :[EXIT]
 INITIALIZE:[EXE]
  ADDRESS(W)
  TARGET=55555567
  PC    =0810000A

So, it's more of a problem with the actual code.  The code is supposed to set up 6 enemies and run a battle system renderloop, in which for now I only render the first enemy (with an enemy index pointing to the "Nomekati" enemy in the bestiary) using the varibles not in the dynamic enemy variable, but rather in the const one in the Bestiary array (since those values aren't supposed to change anyways).  I'm sorry for all of the boring code posted, but I really would sincerely love help figuring out what I did wrong here.  TIA.