Re: garbage collection in bgt

You would need to make it more event-oriented. BGT doesn't come with anything particularly for that purpose, so you'd need your own classes/functions/etc.

The general outline that I use for an event-oriented game lent itself to an interface well enough that I made it one, so I'll just paste it:
(It's ported from Java and uses mappable controls, hence the commented lines and KeyC objects.)

  interface Game {

   void setKeys(KeyC@ p1, KeyC@ p2);
//   void controllerStep(GamePad[] pads); // I'm a little worried about this one on the grounds that it might slow down reaction time for controllers.
   void setFrameData(int tt, double dd);
   void keyPressed(int k);
   void keyReleased(int k);
//   void paint(Graphics2D g);
   void step(double dt);
   int getT();
   double getSpeed();
   void kill();
void toggleBraille();
void paint();
}

When I went looking for that, I found one I wrote to be more BGT specific, which probably explains the concepts better. It's meant to be extended so you can replace the key event and tick methods with whatever you need.


#include "clock.bgt"

class game {
clock@ game_clock;
bool allow_alt_f4=true;

game() {reset(50);}
game(double frames_per_second) {
this.reset(frames_per_second);
}
void reset(double fps) {
clock temp(fps);
@game_clock=temp;
}
void start(string title="BGT Game Window") {
show_game_window(title);
while(true) {
tick(game_clock.delay);
int[] keys=keys_pressed();
for(uint i=0; i<keys.length(); i++) {
this.key_pressed(keys[i]);
}
keys=keys_released();
for(uint i=0; i<keys.length(); i++) {
this.key_released(keys[i]);
}
// So on for mouse and joystick.

game_clock.tick();
}
}
void key_pressed(int k) {
if(allow_alt_f4 and key_down(KEY_LMENU) and k==KEY_F4) exit();
}
void key_released(int k) {}
void tick(double dt) {}
}

(Looking at it, I'm kinda wishing I'd put the call to this.tick below the key handling. It's a subtle thing, but without threading, it means there will be a whole frame between a key event and the next world step. The clock object is a custom class to try and keep the game at a constant speed.)

I eventually wound up making my own menu class and frequently wrote a method for handling common menu operations. For example, you might have these (just typed into the reply box, might contain errors):

enum screens {MAIN, OPTIONS, PAUSED, GAME};

Menu@ mainMenu, optionMenu, pauseMenu;
int up, down, left, right, attack, enter; // controls.
int screen=MAIN;

// (Code...)

void keyPressed(int k) {
    if(screen==MAIN) {
        int result=doMenu(mainMenu, k);
        if(result>=0) {;} // handle menu feedback.
    }
    else if(screen==OPTIONS) {
        int result=doMenu(optionMenu, k);
        // If you don't need these menus to behave differently in ways that can't be caught by the doMenu function, you could just put them in an array or dictionary.
    }
    else if(screen==PAUSED) {
        int result=doMenu(pauseMenu, k);
        if(k==KEY_ESCAPE) screen=GAME; // I'd normally use a function for changing screens, since these usually come with pausing/playing music or other sounds, announcements, etc.
        // I'd probably have doMenu return a value associated with each menu item. It could be which screen to change to.
    }// paused.
    else if(screen==GAME) {
        // Game stuff.
        if(k==KEY_ESCAPE) screen=PAUSED;
    }// Game.
}// key pressed.

int doMenu(Menu@ m, int k) {
    if(@m==null) return -1;
    else if(k==up) m.selectPrevious();
    else if(k==down) m.selectNext();
    else if(k==enter) return menu.getCurrent().value;
    return -1;
}// DoMenu.
_______________________________________________
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
  • ... AudioGames . net Forum — Developers room : audiogamer21 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : audiogamer21 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : audiogamer21 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : CAE_Jones via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : audiogamer21 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : CAE_Jones via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : CAE_Jones via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : CAE_Jones via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : CAE_Jones via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector

Reply via email to