fundamelon / terminal-game-tutorial

step by step game things
MIT License
102 stars 14 forks source link

this code glitches (code up to part 2.2) #15

Closed kkimb003 closed 9 years ago

kkimb003 commented 9 years ago

game.h:

int init();
void run();

void skipMenu(bool);
void setDifficulty(int);
void setTime(int);

main.cpp:

#include "game.h"

intmain(int argv,char** argc)(
    int init_status = init();

    if(init_status != 0)
        run();

    return 0;

}

game.cpp:

#include <string>
#include <ncurses.h>

#include "game.h"

WINDOW* wnd;

int init() {
    wnd = initscr();
    cbreak();
    noecho();
    clear();
    refresh();
    keypad(wnd, true);
    nodelay(wnd, true);
    curs_set(0);
    if(!has_colors()) {
        endwin();
        printf("ERROR: Terminal does not support color.\n");
        exit(1);
    }
    start_color();

    attron(A_BOLD);
    box(wnd, 0, 0);
    attroff(A_BOLD);

    return 0;
}

void run() {

    move(5, 5);

    std::string text = "Hello world!";
    for(size_t i = 0; i < text.size(); i++) {
        addch(text[i]);
        addch(' ');
    }

    refresh();

    while(1);

    endwin();
}
fundamelon commented 9 years ago

init_status returns a 0 if successful. This will be fixed if you change the != on line 6 in main.cpp to ==.

Although a better way to prevent skipping endwin() would be some sort of close function that is called after run() in main, instead of sitting at the end of run().