2017-07-24 48 views
1

我想创建一个窗口,作为我的整个游戏的边界。所以这将是“主要”窗口。我发现最好的方法是创建一个“幽灵”窗口,另一个是真正的窗口,你会使用它,里面的尺寸略小。创造一个“主”窗口与游戏ncurses

但我仍然不能真正地创造它:继承人我最好的解决方案呢。

#include <ncurses.h> 

WINDOW* createWindow(int, int, int, int); 
WINDOW* createRealWindow(int, int, int, int); 

int main() 
{ 
    initscr(); 
    cbreak(); 

    char str[50]; 
    WINDOW *back_window, *real_window; 
    int height=12, width=80; 
    int x=(COLS-width)/2, y=(LINES-height)/2; 

    back_window = createWindow(x, y, width, height); 
    real_window = createRealWindow(x+1, y+1, width, height); 
    wprintw(real_window, "Type: "); 
    wgetstr(real_window, str); 
    wrefresh(real_window); 
    wprintw(real_window, "You typed %s.", str); 
    wrefresh(real_window); 

    napms(5000); 
    endwin(); 

    return 0; 
} 

WINDOW* createWindow(int x, int y, int height, int width) 
{ 
    WINDOW *local; 

    local = newwin(width, height, y, x); 
    box(local, 0, 0); 
    wrefresh(local); 

    return local; 
} 

WINDOW* createRealWindow(int x, int y, int height, int width) 
{ 
    WINDOW *local; 

    local = newwin(width-2, height-2, y+1, x+1); 
    box(local, ' ', ' '); 
    wrefresh(local); 

    return local; 
} 

这里是我的输出: my output

谢谢你们!

编辑:我找到了下面的代码的解决方案,但我不知道这将在其他人不同的解决方案的计算机中表现如何。想法? PS:我用1920×1080

int main() 
{ 
    initscr(); 
    cbreak(); 

    char str[50]; 
    WINDOW *back_window, *real_window; 
    int height=22, width=78; 
    int x=(COLS-width-1)/2, y=(LINES-height-2)/2; 

    back_window = createWindow(x, y, width, height); 
    real_window = createRealWindow(x+1, y+1, width, height); 
    wprintw(real_window, "Type: "); 
    wgetstr(real_window, str); 
    wrefresh(real_window); 
    wprintw(real_window, "You typed %s.", str); 
    wrefresh(real_window); 

    napms(5000); 
    endwin(); 

    return 0; 
} 

WINDOW* createWindow(int x, int y, int height, int width) 
{ 
    WINDOW *local; 

    local = newwin(width+2, height+2, y, x); 
    box(local, 0, 0); 
    wrefresh(local); 

    return local; 
} 

WINDOW* createRealWindow(int x, int y, int height, int width) 
{ 
    WINDOW *local; 

    local = newwin(width, height, y, x); 
    wrefresh(local); 

    return local; 
} 

回答

0

back_window没有显现出来,因为real_window掩盖它(由于尺寸/位置)。但使用subwin是这样做的首选方法:

#include <ncurses.h> 

int main() 
{ 
    initscr(); 
    cbreak(); 

    char str[50]; 
    WINDOW *back_window, *real_window; 
    int height=12, width=80; 
    int x=(COLS-width)/2, y=(LINES-height)/2; 

    back_window = newwin(height, width, y, x); 
    box(back_window, 0, 0); 
    wrefresh(back_window); 

    real_window = subwin(back_window, height-2, width-2, y+1, x+1); 

    wprintw(real_window, "Type: "); 
    wgetstr(real_window, str); 
    wrefresh(real_window); 
    wprintw(real_window, "You typed %s.", str); 
    wrefresh(real_window); 

    napms(5000); 
    endwin(); 

    return 0; 
} 
+0

非常感谢。 你能说这个代码是否会在左上角的不同PC/Resolutions上创建相同的窗口大小? 或者,也许我应该寻找方法来修复终端大小... 非常感谢您的帮助! – NeoFahrenheit

+0

终端大小决定**'COLS' **和**'LINES ** **的值(给定相同的终端大小,您将获得相同的窗口大小)。 –