init commit
This commit is contained in:
3
curses/example_1/Makefile
Normal file
3
curses/example_1/Makefile
Normal file
@ -0,0 +1,3 @@
|
||||
LDFLAGS=-lncurses
|
||||
|
||||
all: demo
|
BIN
curses/example_1/demo
Executable file
BIN
curses/example_1/demo
Executable file
Binary file not shown.
92
curses/example_1/demo.c
Normal file
92
curses/example_1/demo.c
Normal file
@ -0,0 +1,92 @@
|
||||
#include <stdlib.h>
|
||||
#include <ncurses.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define DELAY 30000
|
||||
|
||||
void quit();
|
||||
void draw_borders(WINDOW *screen);
|
||||
|
||||
int main(int argc, char *argv[]){
|
||||
int x=0, y=0;
|
||||
int max_y = 0, max_x = 0;
|
||||
int next_x = 0;
|
||||
int direction = 1;
|
||||
int keypress = 0;
|
||||
|
||||
initscr(); // Initialize window
|
||||
noecho(); // Don't echo keypresses
|
||||
curs_set(FALSE); // No cursor
|
||||
nodelay(stdscr,1); // Stop getch from waiting.
|
||||
//Global var 'stdscr' is created by the call to 'initscr()'
|
||||
getmaxyx(stdscr, max_y, max_x); //Get the max x/y of the screen
|
||||
|
||||
WINDOW *main = newwin(max_y - 3, max_x, 0, 0);
|
||||
WINDOW *sub = newwin(3, max_x, max_y - 3, 0);
|
||||
draw_borders(main);
|
||||
draw_borders(sub);
|
||||
|
||||
|
||||
|
||||
while(1) {
|
||||
getmaxyx(stdscr, max_y, max_x);
|
||||
keypress = getch(); // Listen for exit command
|
||||
clear(); // Clear screen of all previous chars
|
||||
mvprintw(y, x, "o"); // Print our "ball" at the current xy position
|
||||
refresh();
|
||||
|
||||
usleep(DELAY); // Shorter delay between movements
|
||||
|
||||
next_x = x + direction;
|
||||
|
||||
if (keypress == 'q') {
|
||||
quit();
|
||||
}
|
||||
|
||||
if (next_x >= max_x || next_x < 0) {
|
||||
direction*= -1;
|
||||
}
|
||||
else {
|
||||
x+= direction;
|
||||
}
|
||||
mvwin(sub, 50, 50);
|
||||
wrefresh(sub);
|
||||
|
||||
// x++; // Advance the ball to the right
|
||||
}
|
||||
|
||||
// mvprintw(0, 0, "Hello, world!"); //Print some words in the spot 0,0
|
||||
// refresh(); // refresh window so words appear
|
||||
|
||||
// sleep(1); //derp
|
||||
// endwin(); // Restore normal terminal behavior
|
||||
}
|
||||
|
||||
void draw_borders(WINDOW *screen) {
|
||||
int x, y, i;
|
||||
|
||||
getmaxyx(screen, y, x);
|
||||
|
||||
// 4 corners
|
||||
mvwprintw(screen, 0, 0, "+");
|
||||
mvwprintw(screen, y - 1, 0, "+");
|
||||
mvwprintw(screen, 0, x - 1, "+");
|
||||
mvwprintw(screen, y - 1, x - 1, "+");
|
||||
|
||||
// sides
|
||||
for (i = 1; i < (y - 1); i++) {
|
||||
mvwprintw(screen, i, 0, "|");
|
||||
mvwprintw(screen, i, x - 1, "|");
|
||||
}
|
||||
|
||||
// top and bottom
|
||||
for (i = 1; i < (x - 1); i++) {
|
||||
mvwprintw(screen, 0, i, "-");
|
||||
mvwprintw(screen, y - 1, i, "-");
|
||||
}
|
||||
}
|
||||
|
||||
void quit(){
|
||||
endwin();
|
||||
exit(0);
|
||||
}
|
3
curses/example_1/test/Makefile
Normal file
3
curses/example_1/test/Makefile
Normal file
@ -0,0 +1,3 @@
|
||||
LDFLAGS=-lncurses
|
||||
|
||||
all: demo
|
BIN
curses/example_1/test/demo
Executable file
BIN
curses/example_1/test/demo
Executable file
Binary file not shown.
78
curses/example_1/test/demo.c
Normal file
78
curses/example_1/test/demo.c
Normal file
@ -0,0 +1,78 @@
|
||||
#include <ncurses.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void draw_borders(WINDOW *screen) {
|
||||
int x, y, i;
|
||||
|
||||
getmaxyx(screen, y, x);
|
||||
|
||||
// 4 corners
|
||||
mvwprintw(screen, 0, 0, "+");
|
||||
mvwprintw(screen, y - 1, 0, "+");
|
||||
mvwprintw(screen, 0, x - 1, "+");
|
||||
mvwprintw(screen, y - 1, x - 1, "+");
|
||||
|
||||
// sides
|
||||
for (i = 1; i < (y - 1); i++) {
|
||||
mvwprintw(screen, i, 0, "|");
|
||||
mvwprintw(screen, i, x - 1, "|");
|
||||
}
|
||||
|
||||
// top and bottom
|
||||
for (i = 1; i < (x - 1); i++) {
|
||||
mvwprintw(screen, 0, i, "-");
|
||||
mvwprintw(screen, y - 1, i, "-");
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int parent_x, parent_y, new_x, new_y;
|
||||
int score_size = 3;
|
||||
|
||||
initscr();
|
||||
noecho();
|
||||
curs_set(FALSE);
|
||||
|
||||
// set up initial windows
|
||||
getmaxyx(stdscr, parent_y, parent_x);
|
||||
|
||||
WINDOW *field = newwin(parent_y - score_size, parent_x, 0, 0);
|
||||
WINDOW *score = newwin(score_size, parent_x, parent_y - score_size, 0);
|
||||
|
||||
draw_borders(field);
|
||||
draw_borders(score);
|
||||
|
||||
while(1) {
|
||||
getmaxyx(stdscr, new_y, new_x);
|
||||
|
||||
if (new_y != parent_y || new_x != parent_x) {
|
||||
parent_x = new_x;
|
||||
parent_y = new_y;
|
||||
|
||||
wresize(field, new_y - score_size, new_x);
|
||||
wresize(score, score_size, new_x);
|
||||
mvwin(score, new_y - score_size, 0);
|
||||
|
||||
wclear(stdscr);
|
||||
wclear(field);
|
||||
wclear(score);
|
||||
|
||||
draw_borders(field);
|
||||
draw_borders(score);
|
||||
}
|
||||
|
||||
// draw to our windows
|
||||
mvwprintw(field, 1, 1, "Field");
|
||||
mvwprintw(score, 1, 1, "Score");
|
||||
|
||||
// refresh each window
|
||||
wrefresh(field);
|
||||
wrefresh(score);
|
||||
}
|
||||
|
||||
endwin();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
3
curses/fun/Makefile
Normal file
3
curses/fun/Makefile
Normal file
@ -0,0 +1,3 @@
|
||||
LDFLAGS=-lncurses
|
||||
|
||||
all: fun
|
BIN
curses/fun/fun
Executable file
BIN
curses/fun/fun
Executable file
Binary file not shown.
134
curses/fun/fun.c
Normal file
134
curses/fun/fun.c
Normal file
@ -0,0 +1,134 @@
|
||||
#include <stdlib.h>
|
||||
#include <ncurses.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void quit();
|
||||
void draw_borders(WINDOW *screen);
|
||||
int move_player(int keypress, int* player_y, int* player_x);
|
||||
|
||||
void refresh_window(WINDOW *screen);
|
||||
|
||||
|
||||
int main(int argc, char *argv[]){
|
||||
int max_y, max_x, new_y, new_x, field_max_x, field_max_y, player_x, player_y;
|
||||
int next_x = 0;
|
||||
int keypress;
|
||||
int control_size = 5;
|
||||
|
||||
initscr(); // Initialize window
|
||||
noecho(); // Don't echo keypresses
|
||||
curs_set(FALSE); // No cursor
|
||||
nodelay(stdscr,1); // Stop getch from waiting.
|
||||
|
||||
getmaxyx(stdscr, max_y, max_x); //Get the max x/y of the screen
|
||||
|
||||
|
||||
WINDOW *field = newwin(max_y - control_size, max_x, 0, 0);
|
||||
WINDOW *controls = newwin(control_size, max_x, max_y - control_size, 0);
|
||||
|
||||
draw_borders(field);
|
||||
draw_borders(controls);
|
||||
|
||||
|
||||
//center player on field
|
||||
getmaxyx(field, field_max_y, field_max_x);
|
||||
|
||||
player_x = field_max_x/2;
|
||||
player_y = field_max_y/2;
|
||||
|
||||
while(1){
|
||||
getmaxyx(stdscr, new_y, new_x);
|
||||
|
||||
keypress = getch(); // Listen for commands; must be before window refresh
|
||||
|
||||
|
||||
if (new_y != max_y || new_x != max_x) {
|
||||
max_x = new_x;
|
||||
max_y = new_y;
|
||||
|
||||
wresize(field, new_y - control_size, new_x);
|
||||
wresize(controls, control_size, new_x);
|
||||
mvwin(controls, new_y - control_size, 0);
|
||||
wclear(stdscr);
|
||||
wclear(field);
|
||||
wclear(controls);
|
||||
|
||||
draw_borders(field);
|
||||
draw_borders(controls);
|
||||
wrefresh(stdscr);
|
||||
}
|
||||
|
||||
mvwprintw(field, 1, 1, "Field");
|
||||
mvwprintw(field, player_y, player_x, "o");
|
||||
|
||||
mvwprintw(controls, 1, 1, "Commands");
|
||||
mvwprintw(controls, 3, 1, "^Q to quit");
|
||||
|
||||
wrefresh(field);
|
||||
wrefresh(controls);
|
||||
|
||||
if (keypress != -1) {
|
||||
move_player(keypress, &player_y, &player_x);
|
||||
refresh_window(field);
|
||||
}
|
||||
}
|
||||
endwin();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int move_player(int keypress, int* player_y, int* player_x){
|
||||
if (keypress == 'w'){
|
||||
*player_y = *player_y - 1;
|
||||
}
|
||||
|
||||
else if (keypress == 'a'){
|
||||
*player_x = *player_x - 1;
|
||||
}
|
||||
else if (keypress == 's'){
|
||||
*player_y = *player_y + 1;
|
||||
}
|
||||
else if (keypress == 'd'){
|
||||
*player_x = *player_x + 1;
|
||||
}
|
||||
|
||||
else if (keypress == 17) {
|
||||
quit();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void refresh_window(WINDOW *screen) {
|
||||
wclear(screen);
|
||||
draw_borders(screen);
|
||||
wrefresh(screen);
|
||||
}
|
||||
|
||||
void draw_borders(WINDOW *screen) {
|
||||
int x, y, i;
|
||||
|
||||
getmaxyx(screen, y, x);
|
||||
|
||||
// 4 corners
|
||||
mvwprintw(screen, 0, 0, "+");
|
||||
mvwprintw(screen, y - 1, 0, "+");
|
||||
mvwprintw(screen, 0, x - 1, "+");
|
||||
mvwprintw(screen, y - 1, x - 1, "+");
|
||||
|
||||
// sides
|
||||
for (i = 1; i < (y - 1); i++) {
|
||||
mvwprintw(screen, i, 0, "|");
|
||||
mvwprintw(screen, i, x - 1, "|");
|
||||
}
|
||||
|
||||
// top and bottom
|
||||
for (i = 1; i < (x - 1); i++) {
|
||||
mvwprintw(screen, 0, i, "-");
|
||||
mvwprintw(screen, y - 1, i, "-");
|
||||
}
|
||||
}
|
||||
|
||||
void quit(){
|
||||
endwin();
|
||||
exit(0);
|
||||
}
|
Reference in New Issue
Block a user