#include "mainwindow.h"
#include <QApplication>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <cstring>
#include <stdlib.h>
#include <vector>
#include <pthread.h>
#include <string>
#include <iostream>
#include <X11/Xlib.h>
#include "msg.h"
#define NUM_THREADS 1
using namespace std;
class ConnectionVar{
public:
int socket;
int seq;
int playerID;
int x = 0;
int y = 0;
};
class players{
public:
int playerID;
int x = 0;
int y = 0;
};
ConnectionVar var;
void sendInitMessage(){
int results;
var.seq = 0;
JoinMsg join;
join.head.id = 0;
join.head.seq_no = var.seq;
join.head.type = Join;
join.head.length = sizeof(join);
var.seq = var.seq + 1;
char sendBuffer[sizeof(join)];
memcpy((void*)sendBuffer, (void*)&join, sizeof(join));
results = send(var.socket, sendBuffer, sizeof(sendBuffer), 0);
}
void *recvieveMsg(void *arg){
int results;
MainWindow mw;
vector<players> pl;
char recBuffer[1024];
while(1){
results = recv(var.socket, recBuffer, sizeof(recBuffer), 0);
printf("Something recieved");
if(results > 0){
MsgHead* msghead;
msghead = (MsgHead*)recBuffer;
if(msghead->type == Join){
var.playerID = msghead->id;
printf("%d", var.playerID);
}else if(msghead->type == Leave){
printf("LEAVE!!\n");
}else if(msghead->type == Change){
ChangeMsg* changemsg;
changemsg = (ChangeMsg*)recBuffer;
if(changemsg->type == NewPlayer){
NewPlayerMsg* newPlayer;
newPlayer = (NewPlayerMsg*)recBuffer;
if(msghead->id != var.playerID){
players *player = new players();
player->playerID = msghead->id;
pl.push_back(*player);
}
}else if(changemsg->type == PlayerLeave){
printf("Player Leave");
}else if(changemsg->type == NewPlayerPosition){
NewPlayerPositionMsg* newPos;
newPos = (NewPlayerPositionMsg*)recBuffer;
if(msghead->id == var.playerID){
var.x = newPos->pos.x;
var.y = newPos->pos.y;
//mw.pos(1, 1);
}else{
for(int i=0;i<pl.size();i++){
if(pl.at(i).playerID == msghead->id){
pl.at(i).x = newPos->pos.x;
pl.at(i).y = newPos->pos.y;
}
}
}
cout << "Pos" << newPos->pos.x << " "<<newPos->pos.y <<endl;
}else{
printf("Change msg went wrong");
}
}else if(msghead->type == Event){
printf("Event!!\n");
}else if(msghead->type == TextMessage){
printf("TextMessage!!\n");
}else{
printf("What was that?");
}
}else if(results == 0){
printf("Connection closed!!!");
break;
}else{
printf("Something went wrong");
break;
}
fflush(stdout);
}
}
void sendMessage(char* s, MsgType type){
std::cout << s << endl;
if(type == Event){
MoveEvent ev;
ev.event.type = Move;
ev.event.head.id = var.playerID;
ev.event.head.seq_no = var.seq;
ev.event.head.type = Event;
if(strcmp(s, "Left") == 0){
ev.pos.x = var.x -1;
ev.pos.y = var.y;
}else if(strcmp(s, "Right") == 0){
ev.pos.x = var.x + 1;
ev.pos.y = var.y;
}else if(strcmp(s, "Up") == 0){
ev.pos.x = var.x;
ev.pos.y = var.y -1;
}else if(strcmp(s, "Down") == 0){
ev.pos.x = var.x;
ev.pos.y = var.y + 1;
}else{
std::cout << "Something wrong" << endl;
}
ev.event.head.length = sizeof(ev);
char sendBuffer[sizeof(ev)];
memcpy((void*)sendBuffer, (void*)&ev, sizeof(ev));
send(var.socket, sendBuffer, sizeof(sendBuffer), 0);
}
}
void connection(){
int socket, results;
struct addrinfo *serverInfo, hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
results = getaddrinfo("localhost", "49152", &hints, &serverInfo);
if(results != 0){
printf("getaddrinfo went wrong");
exit(1);
}
socket = ::socket(serverInfo->ai_family, serverInfo->ai_socktype, serverInfo->ai_protocol);
var.socket = socket;
results = connect(socket, serverInfo->ai_addr, serverInfo->ai_addrlen);
if(results == -1){
printf("Could not connect");
exit(1);
}
freeaddrinfo(serverInfo);
pthread_t threads[NUM_THREADS];
sendInitMessage();
results = pthread_create(&threads[0], NULL, recvieveMsg, NULL);
}
void startGame(){
connection();
}
int main(int argc, char *argv[])
{
XInitThreads();
MainWindow w;
w.show();
return a.exec();
}
#include "mainwindow.h"
#include <QApplication>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <cstring>
#include <stdlib.h>
#include <vector>
#include <pthread.h>
#include <string>
#include <iostream>
#include <X11/Xlib.h>
#include "msg.h"
#define NUM_THREADS 1
using namespace std;
class ConnectionVar{
public:
int socket;
int seq;
int playerID;
int x = 0;
int y = 0;
};
class players{
public:
int playerID;
int x = 0;
int y = 0;
};
ConnectionVar var;
void sendInitMessage(){
int results;
var.seq = 0;
JoinMsg join;
join.head.id = 0;
join.head.seq_no = var.seq;
join.head.type = Join;
join.head.length = sizeof(join);
var.seq = var.seq + 1;
char sendBuffer[sizeof(join)];
memcpy((void*)sendBuffer, (void*)&join, sizeof(join));
results = send(var.socket, sendBuffer, sizeof(sendBuffer), 0);
}
void *recvieveMsg(void *arg){
int results;
MainWindow mw;
vector<players> pl;
char recBuffer[1024];
while(1){
results = recv(var.socket, recBuffer, sizeof(recBuffer), 0);
printf("Something recieved");
if(results > 0){
MsgHead* msghead;
msghead = (MsgHead*)recBuffer;
if(msghead->type == Join){
var.playerID = msghead->id;
printf("%d", var.playerID);
}else if(msghead->type == Leave){
printf("LEAVE!!\n");
}else if(msghead->type == Change){
ChangeMsg* changemsg;
changemsg = (ChangeMsg*)recBuffer;
if(changemsg->type == NewPlayer){
NewPlayerMsg* newPlayer;
newPlayer = (NewPlayerMsg*)recBuffer;
if(msghead->id != var.playerID){
players *player = new players();
player->playerID = msghead->id;
pl.push_back(*player);
}
}else if(changemsg->type == PlayerLeave){
printf("Player Leave");
}else if(changemsg->type == NewPlayerPosition){
NewPlayerPositionMsg* newPos;
newPos = (NewPlayerPositionMsg*)recBuffer;
if(msghead->id == var.playerID){
var.x = newPos->pos.x;
var.y = newPos->pos.y;
//mw.pos(1, 1);
}else{
for(int i=0;i<pl.size();i++){
if(pl.at(i).playerID == msghead->id){
pl.at(i).x = newPos->pos.x;
pl.at(i).y = newPos->pos.y;
}
}
}
cout << "Pos" << newPos->pos.x << " "<<newPos->pos.y <<endl;
}else{
printf("Change msg went wrong");
}
}else if(msghead->type == Event){
printf("Event!!\n");
}else if(msghead->type == TextMessage){
printf("TextMessage!!\n");
}else{
printf("What was that?");
}
}else if(results == 0){
printf("Connection closed!!!");
break;
}else{
printf("Something went wrong");
break;
}
fflush(stdout);
}
}
void sendMessage(char* s, MsgType type){
std::cout << s << endl;
if(type == Event){
MoveEvent ev;
ev.event.type = Move;
ev.event.head.id = var.playerID;
ev.event.head.seq_no = var.seq;
ev.event.head.type = Event;
if(strcmp(s, "Left") == 0){
ev.pos.x = var.x -1;
ev.pos.y = var.y;
}else if(strcmp(s, "Right") == 0){
ev.pos.x = var.x + 1;
ev.pos.y = var.y;
}else if(strcmp(s, "Up") == 0){
ev.pos.x = var.x;
ev.pos.y = var.y -1;
}else if(strcmp(s, "Down") == 0){
ev.pos.x = var.x;
ev.pos.y = var.y + 1;
}else{
std::cout << "Something wrong" << endl;
}
ev.event.head.length = sizeof(ev);
char sendBuffer[sizeof(ev)];
memcpy((void*)sendBuffer, (void*)&ev, sizeof(ev));
send(var.socket, sendBuffer, sizeof(sendBuffer), 0);
}
}
void connection(){
int socket, results;
struct addrinfo *serverInfo, hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
results = getaddrinfo("localhost", "49152", &hints, &serverInfo);
if(results != 0){
printf("getaddrinfo went wrong");
exit(1);
}
socket = ::socket(serverInfo->ai_family, serverInfo->ai_socktype, serverInfo->ai_protocol);
var.socket = socket;
results = connect(socket, serverInfo->ai_addr, serverInfo->ai_addrlen);
if(results == -1){
printf("Could not connect");
exit(1);
}
freeaddrinfo(serverInfo);
pthread_t threads[NUM_THREADS];
sendInitMessage();
results = pthread_create(&threads[0], NULL, recvieveMsg, NULL);
}
void startGame(){
connection();
}
int main(int argc, char *argv[])
{
XInitThreads();
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
#include "mainwindow.h"
#include "msg.h"
#include "ui_mainwindow.h"
#include <QKeyEvent>
#include <QPainter>
#include <QWidget>
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
gameStatus = false;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_connect_clicked()
{
ui->connect->setEnabled(false);
ui->disconnect->setEnabled(true);
gameStatus = true;
startGame();
}
void MainWindow
::keyPressEvent(QKeyEvent *event
){ if(gameStatus){
switch(event->key()){
case Qt::Key_Left:
sendMessage("Left", Event);
break;
case Qt::Key_Right:
sendMessage("Right", Event);
break;
case Qt::Key_Up:
sendMessage("Up", Event);
break;
case Qt::Key_Down:
sendMessage("Down", Event);
break;
default:
event->ignore();
break;
}
}
}
printf("SDA");
fflush(stdout);
if(gameStatus){
painter.drawLine(115, 115, 5, 5);
}
}
void MainWindow::updateEvent(){
printf("SAJDIDAS");
fflush(stdout);
this->update();
}
void MainWindow::pos(int x, int y){
this->updateEvent();
}
/*
void MainWindow::on_disconnect_clicked()
{
}
*/
#include "mainwindow.h"
#include "msg.h"
#include "ui_mainwindow.h"
#include <QKeyEvent>
#include <QPainter>
#include <QWidget>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
gameStatus = false;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_connect_clicked()
{
ui->connect->setEnabled(false);
ui->disconnect->setEnabled(true);
gameStatus = true;
startGame();
}
void MainWindow::keyPressEvent(QKeyEvent *event){
if(gameStatus){
switch(event->key()){
case Qt::Key_Left:
sendMessage("Left", Event);
break;
case Qt::Key_Right:
sendMessage("Right", Event);
break;
case Qt::Key_Up:
sendMessage("Up", Event);
break;
case Qt::Key_Down:
sendMessage("Down", Event);
break;
default:
event->ignore();
break;
}
}
}
void MainWindow::paintEvent(QPaintEvent *e){
printf("SDA");
fflush(stdout);
if(gameStatus){
QPainter painter(this);
painter.drawLine(115, 115, 5, 5);
}
}
void MainWindow::updateEvent(){
printf("SAJDIDAS");
fflush(stdout);
this->update();
}
void MainWindow::pos(int x, int y){
this->updateEvent();
}
/*
void MainWindow::on_disconnect_clicked()
{
}
*/
To copy to clipboard, switch view to plain text mode
Bookmarks