1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
/*
NOTES:
Anything that has to do with the terminal can be 100% disabled, but there is no difference.
*/
#include "headers/subnet.hpp"
#include "headers/netcode.hpp"
#include "headers/netcrypt.hpp"
#include "headers/terminal.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
using namespace std;
SDL_Surface * stdscr;
bool quit = false;
vector<InputListener*> * listeners;
SDL_Thread * eventThread;
SDL_Thread * inputThread;
InputListener::~InputListener()
{
return;
}
void InputListener::keyDown(SDL_keysym s)
{
return;
}
void InputListener::keyUp(SDL_keysym s)
{
return;
}
void InputListener::keyAction(bool b, SDL_keysym s)
{
return;
}
void handleEvent(SDL_Event * event)
{
vector<InputListener *>::iterator it;
switch (event->type)
{
case SDL_QUIT:
stdterm->print("bye!");
quit = true;
break;
case SDL_KEYDOWN:
for (it = listeners->begin(); it < listeners->end(); it++)
{
if ((*it)->active)
{
if ((*it)->mono) (*it)->keyAction(true, event->key.keysym);
else (*it)->keyDown(event->key.keysym);
}
}
break;
case SDL_KEYUP:
for (it = listeners->begin(); it < listeners->end(); it++)
{
if ((*it)->active)
{
if ((*it)->mono) (*it)->keyAction(false, event->key.keysym);
else (*it)->keyUp(event->key.keysym);
}
}
break;
case SDL_VIDEOEXPOSE:
SDL_Flip(stdscr);
break;
}
}
void handleInput(const char * s)
{
//TODO:
stdterm->print("");
if(strcmp(s,"tutorial")==0)
{
stdterm->print("You need a tutorial? Ha! you noob!");
}else
{
stdterm->print("Unknown command!");
}
}
int t_inputLoop(void * d)
{
listeners->push_back(stdterm);
stdterm->print("Welcome to SubNet! type 'tutorial' or 'commands' for help.");
while (!quit)
{
handleInput(stdterm->getstr());
SDL_Delay(1);
}
return 0;
}
//int t_eventLoop(void * d)
//{
// SDL_Event event;
// cout.flush();
// while (!quit)
// {
// while (SDL_PollEvent(&event))
// handleEvent(&event);
// SDL_Delay(1);
// cout << "still here" << endl;
// }
// return 0;
//}
void loop()
{
SDL_Event event;
//eventThread = SDL_CreateThread(t_eventLoop, NULL);
inputThread = SDL_CreateThread(t_inputLoop, NULL);//Can be commented out no difference!
while (!quit)
{
while (SDL_PollEvent(&event))
handleEvent(&event);
cout.flush();
SDL_Flip(stdscr);
SDL_Delay(1);
}
}
void quitAll()
{
quit_terminal();
quit_net();
SDL_Quit();
}
int main(int argc, char *argv[])
{
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
cout << "Something went wrong in SDL_Init\n";
}
atexit(quitAll);
cout << "Starting up SubNet\n";
listeners = new vector<InputListener*>;
try
{
init_net();
cout << "Netcode init\n";
cout << "Terminal init\n";
SDL_WM_SetCaption("SubNet", NULL);
stdscr = SDL_SetVideoMode(TERM_WIDTH * 8, TERM_HEIGHT * 8, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
init_terminal();
loop();
} catch (const SubNetException& e)
{
cout << e.what() << endl;
}
return 0;
}
|