Jump to content
  • entries
    469
  • comments
    324
  • views
    399,666

Subroutines in C

Serguei2

164 views

I learned something new in C recently:

Theses codes are for Colecovision.


This code is like if I type it in Basic, without numbers.

 



#include <coleco.h>
#include <getput1.h>

 

void nmi(void) {}

void main(void)
{
vdp_out(0,1);
vdp_out(0,0x1F85);
paper(0xc);
screen_mode_2_text();
upload_default_ascii(NORMAL);
duplicate_pattern();
cls();
screen_on();
print_at(1,1,"TESTING");
fill_vram(0x2200,0xe1,256);
loop:
goto loop;
}




This one uses a subroutine.

 



#include <coleco.h>
#include <getput1.h>

 

void nmi(void) {}
void testing(void){
print_at(1,1,"TESTING");
}

void main(void)
{
vdp_out(0,1);
vdp_out(0,0x1F85);
paper(0xc);
screen_mode_2_text();
upload_default_ascii(NORMAL);
duplicate_pattern();
cls();
screen_on();
testing();
fill_vram(0x2200,0xe1,256);
loop:
goto loop;
}



Both codes give the same result except the last one use a subroutine.


When I saw codes from source games for the first time, I was confusing and completely lost.

Now, I'm fine with it and I can go further.

My GOOMW (Get Out Of My Way) is a mess and need to clean up using subroutines instead.


4 Comments


Recommended Comments

void nmi(void) {}
void testing(void){
 print_at(1,1,"TESTING");
}

This is confusing. I don't put anything in the nmi bracket unless I need something happen for the entire game. I never seen a subroutine nested in a nmi{}. Normally sub routine is above the main(), and nmi() is below. You can do 60fps game loop by.

enable_nmi

gameloop:

delay(1);

testing();

goto gameloop;

 

This is safer than putting stuff in NMI because you may run out of cycle if you put everything in that bracket overflowing the stack.

Share this comment


Link to comment

The subroutine is not nested in the nmi function, it just looks like. If you read it carefully, after nmi() there are { and } in same line.

 

C language is free form so it takes it like an empty function.

Share this comment


Link to comment
Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...