You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
259 lines
7.3 KiB
259 lines
7.3 KiB
#include <curses.h>
|
|
#include <stdlib.h>
|
|
#include <ncurses.h>
|
|
#include <time.h>
|
|
#include "project/utils/utils.h"
|
|
|
|
#define TILECOUNT 4
|
|
|
|
const char*TILES[TILECOUNT] = {
|
|
/*0*/ "VOID",
|
|
/*1*/ "WALL",
|
|
/*2*/ "GRASS",
|
|
/*3*/ "START",
|
|
};
|
|
|
|
enum Tile{
|
|
VOID,WALL,GRASS,START
|
|
};
|
|
|
|
char lookupReferenceTile(char*tileName) {
|
|
for (int j=0;j<TILECOUNT;j++) {
|
|
int counter=0;
|
|
while (TILES[j][counter]!='\0') {
|
|
if (tileName[counter]!=TILES[j][counter]) {
|
|
goto next;
|
|
}
|
|
counter++;
|
|
}
|
|
return j;
|
|
next:continue;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
struct Map {
|
|
char**data;
|
|
int width;
|
|
int height;
|
|
};
|
|
|
|
struct Map LoadWorld(char*filename) {
|
|
FILE*f=fopen(filename,"r");
|
|
char refTable[128]={};
|
|
//Expecting a number then a String. Keep reading until we get a newline, at which point the map data begins.
|
|
char c;
|
|
while ((c=fgetc(f))!='\n') {
|
|
//The next character is the reference character/symbol for the defined obj.
|
|
char sym=c;
|
|
c=fgetc(f);
|
|
//Now we start accumulating a string.
|
|
int strLength=0;
|
|
char*str=malloc(strLength);
|
|
while (c!='\n') {
|
|
str=realloc(str,++strLength);
|
|
str[strLength-1]=c;
|
|
c=fgetc(f);
|
|
}
|
|
char refTileNumb=lookupReferenceTile(str);
|
|
printf("Reference Tile %s identified as %c and refers to slot %d.\n",str,sym,refTileNumb);
|
|
refTable[sym]=refTileNumb;
|
|
}
|
|
int height=0;
|
|
int width=0;
|
|
char**data=malloc(sizeof(char*)*0);
|
|
while (!feof(f)) {
|
|
c=fgetc(f);
|
|
width=0;
|
|
data=realloc(data,sizeof(char*)*++height);
|
|
if (height==1) {
|
|
data[height-1]=malloc(0);
|
|
}
|
|
while (c!='\n'&&!feof(f)) {
|
|
data[height-1]=realloc(data[height-1],++width);
|
|
data[height-1][width-1]=refTable[c];
|
|
printf("%c",c);
|
|
c=fgetc(f);
|
|
}
|
|
printf("\n");
|
|
}
|
|
printf("\n======================\n\n");
|
|
return (struct Map){data,width,height};
|
|
}
|
|
|
|
void drawBorder(WINDOW*box) {
|
|
int rows=getmaxy(box)+1;
|
|
int cols=getmaxx(box)+2;
|
|
int x=getbegx(box);
|
|
int y=getbegy(box);
|
|
//mvwprintw(box,3,1,"%d %d %d %d",x,y,rows,cols);
|
|
move(y-1,x-1);
|
|
for (int yy=0;yy<rows+1;yy++) {
|
|
for (int xx=0;xx<cols;xx++) {
|
|
if (xx==0||xx==cols-1) {
|
|
if (yy==0&&xx==0) {
|
|
addch(ACS_ULCORNER);
|
|
} else
|
|
if (yy==0&&xx==cols-1) {
|
|
addch(ACS_URCORNER);
|
|
} else
|
|
if (yy==rows&&xx==0) {
|
|
addch(ACS_LLCORNER);
|
|
} else
|
|
if (yy==rows&&xx==cols-1) {
|
|
addch(ACS_LRCORNER);
|
|
} else {
|
|
addch(ACS_VLINE);
|
|
}
|
|
} else {
|
|
if (yy==0||yy==rows) {
|
|
addch(ACS_HLINE);
|
|
} else {
|
|
move(getcury(stdscr),getcurx(stdscr)+1);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//Returns true if the window was toggled on, or false if it got hidden.
|
|
boolean ToggleWindow(WINDOW**win,int w,int h,int x,int y) {
|
|
if (*win!=NULL) {
|
|
*win=NULL;
|
|
delwin(*win);
|
|
clear();
|
|
return false;
|
|
} else {
|
|
*win=newwin(h,w,y,x);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
void changeColor(int*oldcol,int newcol) {
|
|
attroff(COLOR_PAIR(*oldcol));
|
|
attron(COLOR_PAIR(newcol));
|
|
*oldcol=newcol;
|
|
}
|
|
|
|
//Provide the width and height that the background needs to cover as well as an offset x and y value.
|
|
void drawBackground(int*currentcol,int background_id,int x,int y,int w,int h) {
|
|
int oldcol=*currentcol;
|
|
switch(background_id) {
|
|
case 0:{
|
|
move(y,x);
|
|
for (int yy=0;yy<h;yy++) {
|
|
for (int xx=0;xx<w;xx++) {
|
|
if (xx%2==0) {
|
|
changeColor(currentcol,9);
|
|
} else {
|
|
changeColor(currentcol,10);
|
|
}
|
|
addch(ACS_DIAMOND);
|
|
}
|
|
move(getcury(stdscr)+1,x);
|
|
}
|
|
}break;
|
|
}
|
|
changeColor(currentcol,oldcol);
|
|
}
|
|
|
|
int main(int argc,char**argv) {
|
|
/*int**worldData;
|
|
struct Map m = LoadWorld("maps/start.world");
|
|
printf("Map is %dx%d\n",m.width,m.height);
|
|
for (int y=0;y<m.height;y++) {
|
|
for (int x=0;x<m.width;x++) {
|
|
printf("%d",m.data[y][x]);
|
|
}
|
|
printf("\n");
|
|
}*/
|
|
int*keyLog=calloc(25,sizeof(int));
|
|
unsigned short currentLogCounter=0;
|
|
unsigned int frameCount = 0;
|
|
int rows,cols;
|
|
clock_t lastTime = clock();
|
|
int FRAMETIME = CLOCKS_PER_SEC/60;
|
|
int currentcol=1;
|
|
|
|
initscr();
|
|
|
|
start_color();
|
|
getmaxyx(stdscr,rows,cols);
|
|
cbreak();
|
|
keypad(stdscr,TRUE);
|
|
nodelay(stdscr,TRUE);
|
|
noecho();
|
|
|
|
init_pair(1,COLOR_BLACK,COLOR_BLACK);
|
|
init_pair(2,COLOR_RED,COLOR_BLACK);
|
|
init_pair(3,COLOR_GREEN,COLOR_BLACK);
|
|
init_pair(4,COLOR_YELLOW,COLOR_BLACK);
|
|
init_pair(5,COLOR_BLUE,COLOR_BLACK);
|
|
init_pair(6,COLOR_MAGENTA,COLOR_BLACK);
|
|
init_pair(7,COLOR_CYAN,COLOR_BLACK);
|
|
init_pair(8,COLOR_WHITE,COLOR_BLACK);
|
|
init_pair(9,COLOR_GREEN,COLOR_WHITE);
|
|
init_pair(10,COLOR_BLACK,COLOR_GREEN);
|
|
changeColor(¤tcol,6);
|
|
|
|
WINDOW*messageBox=newwin(4,cols-2,rows-5,1);
|
|
drawBorder(messageBox);
|
|
//box(messageBox,0,0);
|
|
|
|
boolean resizeOccured=false;
|
|
|
|
refresh();
|
|
getmaxyx(stdscr,rows,cols);
|
|
int ch=ERR;
|
|
while (true) {
|
|
if ((ch=getch())!=ERR) {
|
|
keyLog[currentLogCounter]=ch;
|
|
currentLogCounter=(currentLogCounter+1)%25;
|
|
switch (ch) {
|
|
case 'A':
|
|
case 'a':{
|
|
ToggleWindow(&messageBox,cols-2,4,1,rows-5);
|
|
}break;
|
|
case KEY_RESIZE:{
|
|
resizeOccured=true;
|
|
getmaxyx(stdscr,rows,cols);
|
|
}break;
|
|
case KEY_RIGHT:{
|
|
changeColor(¤tcol,(currentcol+1)%8);
|
|
}break;
|
|
case KEY_LEFT:{
|
|
changeColor(¤tcol,(currentcol-1>=0)?currentcol-1:7);
|
|
}break;
|
|
}
|
|
}
|
|
if (clock()-lastTime>FRAMETIME) {
|
|
if (resizeOccured) {
|
|
resizeOccured=false;
|
|
clear();
|
|
delwin(messageBox);
|
|
messageBox=newwin(4,cols-2,rows-5,1);
|
|
}
|
|
//mvprintw(5,7,"There are %dx%d squares. (%d)",cols,rows,frameCount++);
|
|
drawBackground(¤tcol,0,0,0,cols-1,rows);
|
|
if (messageBox!=NULL) {
|
|
drawBorder(messageBox);
|
|
mvwprintw(messageBox,0,0,"There are %dx%d squares. (%d) It is good!",cols,rows,frameCount);
|
|
}
|
|
for (int i=0;i<25;i++) {
|
|
if (keyLog[i]>0) {
|
|
mvprintw(6+i,2,"Key %d was pressed.",keyLog[i]);
|
|
short r,g,b;
|
|
color_content(currentcol,&r,&g,&b);
|
|
mvprintw(6+i,30,"%d %d %d",r,g,b);
|
|
}
|
|
}
|
|
refresh();
|
|
wrefresh(messageBox);
|
|
lastTime=clock();
|
|
frameCount++;
|
|
}
|
|
}
|
|
free(keyLog);
|
|
endwin();
|
|
return 0;
|
|
}
|
|
|