#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 ;
}

最新game的更多相关文章

  1. 终于等到你:CYQ.Data V5系列 (ORM数据层)最新版本开源了

    前言: 不要问我框架为什么从收费授权转到免费开源,人生没有那么多为什么,这些年我开源的东西并不少,虽然这个是最核心的,看淡了就也没什么了. 群里的网友:太平说: 记得一年前你开源另一个项目的时候我就说 ...

  2. 最新的 cocoaPods 安装方法

    经过努力终于发现了最新的 解决cocoaPods安装的办法: taobao Gems 源已停止维护,现由 ruby-china 提供镜像服务 第一步:安装rvm, 不管需不需要升级ruby,rvm可以 ...

  3. 最新Linux部署.NET,Mono and DNX

    这几天一直在折腾在Linux下的ASP.NET 5,就下在看来在其它操作系统中ASP.NET 5或.NET应用,要想在完整的MS VM(CoreCLR)上运行还不远远达不到,应用的效果. 目前只能在M ...

  4. 吐血大奉献,打造cnblogs最新最火辣的css3模板(IE9以下请勿入内) -- 第一版

    一直自己都想给自己的博客打造一个独一无二的皮肤,但是一直没有强劲的动力去完成这件事情.后来凭借着工作上面的需求(涉及到css3),就把自己的博客当成一个最好的试验场地.从而产生了你现在所看到的这个模板 ...

  5. CentOS 6.6 升级GCC G++ (当前最新版本为v6.1.0) (完整)

    ---恢复内容开始--- CentOS 6.6 升级GCC G++ (当前最新GCC/G++版本为v6.1.0) 没有便捷方式, yum update....   yum install 或者 添加y ...

  6. Oracle 11.2.0.4 RAC安装最新PSU补丁

    环境:两节点RAC(RHEL 6.4 + GI 11.2.0.4 + Oracle 11.2.0.4) 需求:安装最新PSU补丁11.2.0.4.7 1.下载补丁和最新OPatch 2.检查数据库当前 ...

  7. 使用 NuGet 下载最新的 Rafy 框架及文档

    为了让开发者更方便地使用 Rafy 领域实体框架,本月,我们已经把最新版本的 Rafy 框架程序集发布到了 nuget.org 上,同时,还把 RafySDK 的最新版本发布到了 VisualStud ...

  8. 利用TortoiseSVN获取最新版本的OpenCV源码

    转自: http://blog.csdn.net/vsooda/article/details/7555969 1.下载安装TortoiseSVN:http://tortoisesvn.net/dow ...

  9. npm更新到最新版本的方法

    打开命令行工具 npm -v 查看是否是最新版本 如果不是 运行npm i npm g 升级 打开C:\Users\用户名用户目录找到node_modules 文件夹下的npm文件夹,复制一份 打开n ...

  10. 最新GHOST XP系统下载旗舰增强版 V2016年

    系统来自:系统妈:http://www.xitongma.com 深度技术GHOST xp系统旗舰增强版 V2016年3月 系统概述 深度技术ghost xp系统旗舰增强版集合微软JAVA虚拟机IE插 ...

随机推荐

  1. linux操作系统cron详解

    Linux操作系统定时任务系统 Cron 入门 cron是一个linux下的定时执行工具,可以在无需人工干预的情况下运行作业.由于Cron 是Linux的内置服务,但它不自动起来,可以用以下的方法启动 ...

  2. JAVA LinkedList和ArrayList的使用及性能分析

    第1部分 List概括List的框架图List 是一个接口,它继承于Collection的接口.它代表着有序的队列.AbstractList 是一个抽象类,它继承于AbstractCollection ...

  3. Android 判断数据库中是否存在某个表

    public boolean tabIsExist(String tabName){ boolean result = false; if(tabName == null){ return false ...

  4. JSON 入门

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族 ...

  5. Tomcat源码分析--转

    一.架构 下面谈谈我对Tomcat架构的理解 总体架构: 1.面向组件架构 2.基于JMX 3.事件侦听 1)面向组件架构 tomcat代码看似很庞大,但从结构上看却很清晰和简单,它主要由一堆组件组成 ...

  6. linux device driver —— 环形缓冲区的实现

    还是没有接触到怎么控制硬件,但是在书里看到了一个挺巧妙的环形缓冲区实现. 此环形缓冲区实际为一个大小为bufsize的一维数组,有一个rp的读指针,一个wp的写指针. 在数据满时写进程会等待读进程读取 ...

  7. Java基础知识强化之集合框架笔记19:List集合迭代器使用之 并发修改异常的产生原因 以及 解决方案

    1. 我有一个集合,如下,请问,我想判断里面有没有"world"这个元素,如果有,我就添加一个"javaee"元素,请写代码实现. ConcurrentModi ...

  8. css 权威指南笔记( 五)结构和层叠之三种样式来源

    CSS中的样式一共有三种来源:创作人员.读者和用户代理,来源的不同会影响到样式的层叠方式 首先,创作人员(author's+style)样式应该是我们最熟悉的,如果你是一个前端开发者,那么你写的那些样 ...

  9. hadoop集群环境搭建准备工作

    一定要注意hadoop和linux系统的位数一定要相同,就是说如果hadoop是32位的,linux系统也一定要安装32位的. 准备工作: 1 首先在VMware中建立6台虚拟机(配置默认即可).这是 ...

  10. 给Sublime Text2安装轻量级代码提示插件:SublimeCodeIntel

    步骤: 1.下载SublimeCodeIntel(地址https://github.com/SublimeCodeIntel/SublimeCodeIntel): 2.将下载的压缩包解压,并放置在Pa ...