#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. Media Queries 自适应布局展示

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. 如何优化cocos2d程序的内存使用和程序大小:第二部分_(转)

    减少你的程序的大小 把纹理的颜色位深度减少到16位,不仅可以减少内存压力,还可以有效地减少程序的体积.但是,我们还有其它方法可以更进一步地减少程序的大小. TexturePacker PNG 图片优化 ...

  3. Windows 服务卸载之后 重新安装提示 “指定的服务已标记为删除”

    背景:        将一个项目做成一个windows服务,在调试的时候,需要卸载.安装该服务,但提示下面的错误:“指定的服务已标记为删除”,进入服务管理界面,启动自己注册的服务,无法手动更改成启用模 ...

  4. Struts 有哪些经常使用标签库

    Struts 有哪些经常使用标签库 1.html标签库 2.bean标签库 3.logic标签库

  5. Assigning retained object to unsafe property;object will be released after assignment

    解决方法,将变量 @property (assign) UILabel *titleView; 改为 @property (retain) UILabel *titleView;

  6. LNMP一键安装包 V1.1 通告

    LNMP一键安装包 是一个用Linux Shell编写的能够为CentOS/RadHat.Debian/Ubuntu VPS(VDS)或独立主机安装LNMP(Nginx.MySQL/MariaDB.P ...

  7. Qt的皮肤设计(Style Sheet)

      Qt的皮肤设计,也可以说是对Qt应用程序的界面美化,Qt使用了一种类CSS的样式规则QSS. 一.Style Sheet的应用 1.直接在程序代码中设置样式,利用setStyleSheet()方法 ...

  8. Annotation Type @bean,@Import,@configuration使用--官方文档

    @Target(value={METHOD,ANNOTATION_TYPE}) @Retention(value=RUNTIME) @Documented public @interface Bean ...

  9. 基于HTML5的SLG游戏开发(一):搭建开发环境(2)

          游戏开发过程中经常需要处理各种事件,而HTML5游戏开发中,所有的场景和UI面板都是绘制在Canvas上面,假设需要对某一UI面板上的关闭按钮添加事件监听,采取的方法是对关闭按钮图片资源进 ...

  10. Microsoft Windows Server 2008 R2 IIS7.5安装指南

    一.IIS安装步骤: 1.安装Windows Server 2008 R2(见 附录一) 2.配置计算机名称和IP地址(见 附录一) 3.配置成员服务器(见 附录一) 4.点击任务栏上的“服务器管理器 ...