最新game

时间:2021-04-05 19:07:33
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include "game.h"
#include "engine.h"
#include "message.h"
#include "member.h"
#include <ncurses.h>
#include "timer.h"
#include <errno.h> int gqid=;
int current_row=;
int current_col=;
int message_row=;
int message_col=;
WINDOW* message_win =NULL;
WINDOW* input_win =NULL; void game_init()
{
engine_init();
gqid=message_init();
init_member_list();
signal(SIGINT, signal_handle);
signal(EXIT_SIGNAL, signal_handle); game_create_message_win();
game_create_input_win();
create_thread((void*) game_refresh_member); STRUCT_MEMBER member;
member.type=;
member.name="sysAdmin";
regist_member(member); move(, );
current_row=;
current_col=;
} void game_create_message_win()
{
message_win=newwin(, , , );
if(message_win !=NULL)
{
refresh();
box(message_win, , );
wrefresh(message_win);
}
} void game_start()
{
} void game_create_input_win()
{
refresh();
input_win=newwin(, , , );
if(message_win !=NULL)
{
refresh();
box(input_win, , );
wrefresh(input_win);
mvwprintw(input_win,,,"[membername] say:");
wrefresh(input_win);
}
} void game_refresh_member()
{ int i=;
int y =;
int x=;
int rcv=;
STRUCT_MEMBER_LIST list;
list = get_members();
STRUCT_MSG_BUF msg={}; for(;;){
y=;
x=; mvprintw(y,x,"MEMBER %d", list.number);
for(i=;i<list.number;i++)
{
mvprintw(++y,x, "[name= %s] [type= %d]",list.members[i].name, list.members[i].type);
}
refresh(); rcv = game_receive_msg(&msg); if(rcv > )
{
mvwprintw(message_win,message_row,message_col,"[%d,%d,%s]", msg.length,msg.type,msg.data);
//scrollok(message_win, TRUE);
//scroll(message_win);
wrefresh(message_win);
refresh();
message_row++;
} move(current_row,current_col);
interval_time(INTERVAL);
}
} void game_run()
{
char line[]="";
int ch=;
STRUCT_MSG_BUF msg;
for(;;)
{
//getnstr(line, 70);
//move(current_row,current_col);
//printw("%s", line); ch=getch(); switch(ch)
{
case 'q':
raise(SIGINT);
break;
case '\n':
mvwprintw(input_win,,,"[membername] say:");
mvwprintw(input_win,,," ");
wrefresh(input_win);
move(,);
game_send_msg(line, ENUM_MSG_NORMAL);
memset(line, , strlen(line));
//game_receive_msg(&msg);
break;
default:
if(current_col<)
{
move(current_row,current_col);
printw("%c", ch);
refresh();
line[strlen(line)]=ch;
}
break;
} getyx(stdscr, current_row, current_col);
}
} int game_abort(char* msg)
{
engine_shut();
//fprintf(stderr, "%s\n", msg);
exit(EXIT_FAILURE);
} //
void game_over()
{
engine_shut();
exit(EXIT_SUCCESS);
} void game_send_msg(char* pmsg_content, ENUM_MSG_TYPE msgType)
{
STRUCT_MSG_BUF msg={};
memset(&msg, , sizeof(STRUCT_MSG_BUF));
msg.length=strlen(pmsg_content);
msg.type=msgType;
strncpy(msg.data, pmsg_content, strlen(pmsg_content));
message_send(gqid, &msg, );
} int game_receive_msg(STRUCT_MSG_BUF* pmsg)
{
int ret =-;
if(pmsg != NULL)
{
memset(pmsg, , sizeof(STRUCT_MSG_BUF));
ret = message_receive(gqid, pmsg, IPC_NOWAIT);
} //printw("receive msg %s", pmsg->data);
//refresh(); return ret;
} void print_info()
{ } void signal_handle(int signal)
{
switch(signal)
{
case SIGINT:
game_over();
break;
case EXIT_SIGNAL:
game_over();
break;
default:
break;
} }
#ifndef __GAME_H
#define __GAME_H #include "message.h" #define FRAME_ROW 0
#define FRAME_COL 0 #define MSG_BEGIN_COL 3
#define MSG_END_COL #define EXIT_SIGNAL 1818 typedef enum tag_mode
{
GAME_INIT,
GAME_RUN,
GAME_ABORT,
GAME_OVER
}ENUM_GAME_MODE; typedef struct tag_game
{
ENUM_GAME_MODE mode;
int length;
}STRUCT_GAME; void game_init();
void game_start();
int game_abort(char* msg);
void game_over();
void game_run();
//void game_send_msg();
void game_send_msg(char* pmsg_content, ENUM_MSG_TYPE msgType);
int game_receive_msg(STRUCT_MSG_BUF* pmsg);
void game_show_frame();
void signal_handle(int signal);
void game_create_input_win();
void game_refresh_member();
void game_create_message_win(); #endif
#ifndef __MESSAGE_H
#define __MESSAGE_H #include <sys/msg.h>
#include <sys/types.h>
#include <sys/ipc.h> #define MSG_PATH "./msg/msg"
#define MSG_PJID 1818
#define MAX_MSG_LENGTH 256 typedef enum tag_msg_type
{
ENUM_MSG_REGIST_MEM = ,
ENUM_MSG_UNRGIST_MEM,
ENUM_MSG_NORMAL
}ENUM_MSG_TYPE; typedef struct tag_msg
{
int length;
ENUM_MSG_TYPE type;
char data[MAX_MSG_LENGTH];
} STRUCT_MSG_BUF; int message_init(); int message_receive(int msgid, STRUCT_MSG_BUF* pmsg, int flag);
//int message_send(int msgid, const STRUCT_MSG_BUF* pmsg, int flag)
int message_send(int msgid, const STRUCT_MSG_BUF* pmsg, int flag); #endif
#include <stdio.h>
#include <sys/msg.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include "message.h" int qid=; int message_init()
{ key_t key = ftok(MSG_PATH, MSG_PJID); if(key == -)
{
perror("ftok failed");
exit(EXIT_FAILURE);
} if((qid = msgget(key, IPC_CREAT | )) == -)
{
perror("create message queue failed");
exit(EXIT_FAILURE);
} return qid; } int message_send(int msgid, const STRUCT_MSG_BUF* pmsg, int flag)
{
if( pmsg != NULL && pmsg->length > )
{
if( msgsnd(msgid, pmsg, sizeof(STRUCT_MSG_BUF), ) == -)
{
perror("send msg to message queue failed");
exit(EXIT_FAILURE);
}
}
return ;
} int message_receive(int msgid, STRUCT_MSG_BUF* pmsg, int flag)
{
if( msgrcv(msgid, pmsg, sizeof(STRUCT_MSG_BUF), , flag) == - )
{
perror("receive msg from message queue failed");
exit(EXIT_FAILURE);
}
return ;
}
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h> pthread_t create_thread( void* pFunc)
{ pthread_t tid;
if(pthread_create(&tid, NULL, (void*)pFunc, NULL) == )
{
//fprintf(stdout, "create thread success!\n");
}else
{
//fprintf(stderr, "create thread failed!\n");
exit(EXIT_FAILURE);
} return tid;
}
#ifndef __THREAD_H
#define __THREAD_H #include "pthread.h"
pthread_t create_thread(void* pFunc); #endif
/**@file timer.c
*
* Timer for game
*/ #include <unistd.h>
#include "timer.h" int interval_time(long useconds)
{
if(useconds >= MAX_USECONDS)
return -; return usleep((useconds_t)useconds);
}
#ifndef __TIMER_H
#define __TIMER_H #define MAX_USECONDS 1000000
#define INTERVAL 160000 int interval_time(long useconds); #endif
#include <stdio.h>
#include <ncurses.h>
#include <unistd.h>
#include "game.h"
#include "argument.h"
#include "engine.h"
#include "daemon.h"
#include "timer.h"
#include "message.h" STRUCT_GAME struct_game; int main(int argc, char* argv[])
{
if(argc >)
args_handle(argc, argv); struct_game.mode=GAME_INIT;
// init game
game_init(); while(==)
{
switch(struct_game.mode)
{
case GAME_INIT:
game_start();
struct_game.mode=GAME_RUN;
break;
case GAME_RUN:
game_run();
struct_game.mode=GAME_OVER;
break;
case GAME_ABORT:
game_abort("GAME is ABORTED\n");
break;
case GAME_OVER:
game_over();
break;
default:
fprintf(stdout, "MODE = [%d]\n", struct_game.mode);
break;
} }
//message_init();
return ;
}