1
1
Fork 0
Browse Source

Fix crash and add mouse control

thread-physics
alistair 4 years ago
parent
commit
7063f70ff2
  1. 18
      draw.c
  2. 2
      draw.h
  3. 24
      game.c
  4. 38
      main.c

18
draw.c

@ -65,9 +65,26 @@ void draw_player(SDL_Renderer * ren, int x, int y, bool red) { @@ -65,9 +65,26 @@ void draw_player(SDL_Renderer * ren, int x, int y, bool red) {
} else {
SDL_SetRenderDrawColor(ren, 120, 85, 188, 255);
}
SDL_RenderDrawRect(ren, &player_rect);
SDL_RenderFillRect(ren, &player_rect);
// draw strings
for (int i = 0; i < player.physics->num_strings; i++) {
if (!player.physics->strings[i].attached) {
continue;
}
printf("Draw string");
Vect B;
B = in_view(player.physics->position);
Vect E;
E = in_view(player.physics->strings[i].end_point);
SDL_RenderDrawLine(ren, B.x, B.y, E.x, E.y);
}
SDL_SetRenderDrawColor(ren, 0,0,0, 255);
}
void draw_floor(SDL_Renderer *ren, FloorPoly *poly, bool down) {
@ -308,7 +325,6 @@ void redraw_buffer(SDL_Renderer * ren) { @@ -308,7 +325,6 @@ void redraw_buffer(SDL_Renderer * ren) {
draw_collision_poly(ren, thing.floor->polys[i].physics);
}
break;
}
}

2
draw.h

@ -5,7 +5,7 @@ @@ -5,7 +5,7 @@
#ifndef _DEFDRAW
#define _DEFDRAW
#define SHOWCOLLISION 1
#define SHOWCOLLISION 0
#define SHOWFORCES 0
#include "garbo.h"

24
game.c

@ -602,7 +602,6 @@ void winch_motor_update (Motor* motor) { @@ -602,7 +602,6 @@ void winch_motor_update (Motor* motor) {
Body *body = motor->end_object;
uint32_t now = SDL_GetTicks();
if (body->strings[0].max_length < 0.1
|| !body->strings[0].attached) {
motor->stop = true;
@ -639,7 +638,8 @@ void stop_pull_rope(void) { @@ -639,7 +638,8 @@ void stop_pull_rope(void) {
player.physics->motors[M_WINCH].stop = true;
}
void add_rope(void) {
void add_rope(int mouse_x) {
if (player.physics->strings[0].attached) {
return;
}
@ -652,6 +652,10 @@ void add_rope(void) { @@ -652,6 +652,10 @@ void add_rope(void) {
}
end.y = 0;
if (mouse_x != 0) {
end.x = mouse_x + viewport_pos.x;
}
Floor *ceiling = world.uniques_index[CEILING]->floor;
int y = 0;
@ -1064,7 +1068,7 @@ GlobWorld create_world() { @@ -1064,7 +1068,7 @@ GlobWorld create_world() {
memset(c.things, 0, sizeof(world_thing) * 100);
c.capacity = 100;
c.size = 0;
c.uniques_index = calloc(3, sizeof(world_thing*));
c.uniques_index = calloc(10, sizeof(world_thing*));
return c;
}
@ -1119,7 +1123,7 @@ void handle_input_event(SDL_Event event) { @@ -1119,7 +1123,7 @@ void handle_input_event(SDL_Event event) {
switch (event.type) {
case SDL_KEYDOWN:
if (event.key.keysym.scancode == input_map.player_rope) {
add_rope();
add_rope(0);
} if (sc == input_map.player_up) {
walk_player(0, -1);
} if ( sc == input_map.player_left) {
@ -1143,6 +1147,18 @@ void handle_input_event(SDL_Event event) { @@ -1143,6 +1147,18 @@ void handle_input_event(SDL_Event event) {
stop_pull_rope();
}
break;
case SDL_MOUSEBUTTONDOWN:
if (event.button.button == SDL_BUTTON_LEFT)
add_rope(event.button.x);
if (event.button.button == SDL_BUTTON_RIGHT)
pull_rope(900);
break;
case SDL_MOUSEBUTTONUP:
if (event.button.button == SDL_BUTTON_LEFT)
delete_rope();
if (event.button.button == SDL_BUTTON_RIGHT)
stop_pull_rope();
break;
}
}

38
main.c

@ -25,7 +25,8 @@ struct SDL_Window* make_window(void) { @@ -25,7 +25,8 @@ struct SDL_Window* make_window(void) {
return SDL_CreateWindow("sdl_tester",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
screen_width, screen_height, SDL_WINDOW_FULLSCREEN_DESKTOP);
0, 0,
SDL_WINDOW_FULLSCREEN_DESKTOP);
}
void draw_pictures(struct SDL_Renderer * ren) {
@ -64,7 +65,7 @@ int physics_loop(void *ptr) { @@ -64,7 +65,7 @@ int physics_loop(void *ptr) {
}
}
int main () {
int game(void) {
LOGLEVEL = DEBUG;
STDOUTLEVEL = DEBUG;
@ -90,30 +91,37 @@ int main () { @@ -90,30 +91,37 @@ int main () {
SDL_Thread *physics_thread;
int ignore;
physics_thread = SDL_CreateThread(physics_loop, "Physics", (void *)NULL);
// physics_thread = SDL_CreateThread(physics_loop, "Physics", (void *)NULL);
bool once = true;
startgame(ren);
while (!close) {
SDL_Event event;
while(SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
close = 1;
return 0;
case SDL_KEYDOWN:
case SDL_KEYUP:
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
handle_input_event (event);
}
}
/* Redraw Screen */
redraw(ren);
step(10);
}
SDL_Event event;
SDL_PollEvent(&event);
switch (event.type) {
case SDL_QUIT:
close = 1;
break;
default:
handle_input_event (event);
break;
}
}
}
int main () {
game();
SDL_Quit();
//empty_cleanup_queue();
logwrite(DEBUG, "Emptied cleanup queue\n");
return 0;
}

Loading…
Cancel
Save