diff --git a/netwar.c b/netwar.c
index 2419456d87d0dea2265c5f9e47422286359258ab..b7c2222b212be1a1511e8a0af5167770053ab1a6 100644 (file)
--- a/netwar.c
+++ b/netwar.c
#define HEIGHT 480
static const float ship[] = {
#define HEIGHT 480
static const float ship[] = {
- +1.0, +0.0, -1.0, -1.0,
- -1.0, -1.0, +0.0, +0.0,
- +0.0, +0.0, -1.0, +1.0,
- -1.0, +1.0, +1.0, +0.0,
+ +1.0, +0.0,
+ -1.0, -1.0,
+ +0.0, +0.0,
+ -1.0, +1.0,
+ +1.0, +0.0,
+};
+
+static const float star[] = {
+ +1.0, +0.0,
+ +0.0, -1.0,
+ -1.0, +0.0,
+ +0.0, +1.0,
+ +1.0, +0.0,
};
static char * host = "localhost";
static char * nick = "Anonymous";
};
static char * host = "localhost";
static char * nick = "Anonymous";
-static uint16_t port = DEFAULT_PORT;
+static uint16_t port = PROTOCOL_PORT;
static SDL_Window * window;
static SDL_Renderer * renderer;
static SDL_Window * window;
static SDL_Renderer * renderer;
SDL_DestroyWindow(window);
}
SDL_DestroyWindow(window);
}
+static void paintmodel(const float * model, int modelsz, float mx, float my, float r, float size, int cx, int cy) {
+ int scale = (WIDTH + HEIGHT) / 2 * size / 2;
+
+ int count = modelsz / sizeof(model[0]) / 2;
+ int x = mx * WIDTH / 2;
+ int y = my * HEIGHT / 2;
+
+ SDL_Point pix[count];
+ for(int j = 0; j < count * 2; j += 2) {
+ float c = cos(r * 2 * M_PI);
+ float s = sin(r * 2 * M_PI);
+ float rx = model[j + 0] * scale;
+ float ry = model[j + 1] * scale;
+ pix[j / 2].x = cx + x + (c * rx - s * ry);
+ pix[j / 2].y = cy + y + (s * rx + c * ry);
+ }
+
+ SDL_RenderDrawLines(renderer, pix, count);
+}
+
static void paintwindow() {
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);
SDL_RenderClear(renderer);
int cx = WIDTH / 2;
int cy = HEIGHT / 2;
static void paintwindow() {
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);
SDL_RenderClear(renderer);
int cx = WIDTH / 2;
int cy = HEIGHT / 2;
- int shipsz = (WIDTH + HEIGHT) / 2 * PLAYER_SIZE;
+
+ SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0xFF, 0x00);
+
+ static float start_r;
+ for(int i = 0; i < STAR_SIZE * 200; i++)
+ paintmodel(star, sizeof(star), 0, 0, M_PI / i + start_r, STAR_SIZE, cx, cy);
+
+ start_r += 0.0001;
for(int i = 0; i < MAX_PLAYERS; i++) {
if(i == cl_playerid)
for(int i = 0; i < MAX_PLAYERS; i++) {
if(i == cl_playerid)
else
SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0x00);
else
SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0x00);
- int count = sizeof(ship) / sizeof(ship[0]) / 2;
- int x = g_player[i].x * WIDTH / 2;
- int y = g_player[i].y * HEIGHT / 2;
-
- SDL_Point pixship[count];
- for(int j = 0; j < count * 2; j += 2) {
- float c = cos(g_player[i].r * 2 * M_PI);
- float s = sin(g_player[i].r * 2 * M_PI);
- float rx = ship[j + 0] * shipsz;
- float ry = ship[j + 1] * shipsz;
- pixship[j / 2].x = cx + x + (c * rx - s * ry);
- pixship[j / 2].y = cy + y + (s * rx + c * ry);
- }
-
if(g_player[i].live)
if(g_player[i].live)
- SDL_RenderDrawLines(renderer, pixship, count);
+ paintmodel(ship, sizeof(ship), g_player[i].x, g_player[i].y, g_player[i].r, PLAYER_SIZE, cx, cy);
+ }
+
+ for(int i = 0; i < MAX_BULLETS; i++) {
+ if(g_bullet[i].owner == cl_playerid)
+ SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0x00);
+ else
+ SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0x00);
+
+ int x = g_bullet[i].x * WIDTH / 2;
+ int y = g_bullet[i].y * HEIGHT / 2;
+
+ if(g_bullet[i].live)
+ SDL_RenderDrawPoint(renderer, cx + x, cy + y);
}
SDL_RenderPresent(renderer);
}
SDL_RenderPresent(renderer);
static void keyboardhandle() {
SDL_PumpEvents();
const Uint8 * key = SDL_GetKeyboardState(NULL);
static void keyboardhandle() {
SDL_PumpEvents();
const Uint8 * key = SDL_GetKeyboardState(NULL);
- if(key[SDL_SCANCODE_UP])
- cl_move(DOES_UP);
- if(key[SDL_SCANCODE_DOWN])
- cl_move(DOES_DOWN);
- if(key[SDL_SCANCODE_LEFT])
- cl_move(DOES_LEFT);
- if(key[SDL_SCANCODE_RIGHT])
- cl_move(DOES_RIGHT);
- if(key[SDL_SCANCODE_LCTRL])
- cl_move(DOES_FIRE);
+ cl_move((DoesBits) {
+ .up = key[SDL_SCANCODE_UP],
+ .down = key[SDL_SCANCODE_DOWN],
+ .left = key[SDL_SCANCODE_LEFT],
+ .right = key[SDL_SCANCODE_RIGHT],
+ .fire = key[SDL_SCANCODE_LCTRL],
+ });
}
int main(int argc, char ** argv) {
}
int main(int argc, char ** argv) {
cl_connect(host, port);
cl_connect(host, port);
- Uint32 lastTime = SDL_GetTicks();
- Uint32 currTime = SDL_GetTicks();
-
- while(cl_isrun()) {
+ while(cl_playerid >= 0) {
SDL_Event event;
while(SDL_PollEvent(&event))
if(event.type == SDL_QUIT)
goto cleanup;
SDL_Event event;
while(SDL_PollEvent(&event))
if(event.type == SDL_QUIT)
goto cleanup;
- if(currTime - lastTime >= TICK_DELAY) {
- keyboardhandle();
- g_update();
- lastTime = SDL_GetTicks();
- }
- currTime = SDL_GetTicks();
+ keyboardhandle();
- cl_recv();
+ cl_handle();
paintwindow();
paintwindow();