[Fix] Just a few optimization.
This commit is contained in:
parent
54beb595dc
commit
066d4871cc
23
src/ai.c
23
src/ai.c
@ -8,6 +8,29 @@
|
|||||||
#include "pilot.h"
|
#include "pilot.h"
|
||||||
#include "ai.h"
|
#include "ai.h"
|
||||||
|
|
||||||
|
// == AI ======================================================
|
||||||
|
// -- AI will follow basic tasks defined from Lua AI scripts.
|
||||||
|
// -- If task is NULL, AI will run "control" task.
|
||||||
|
// -- Task is continued every frame.
|
||||||
|
// -- "control" task is a special task that *must* exist in
|
||||||
|
// any given Pilot AI (missiles, and suck will use "seek".
|
||||||
|
// -- "control" task is not permanent, but transitory.
|
||||||
|
// -- "control" task sets another task.
|
||||||
|
// -- "control" task is also run at a set rate (depending on
|
||||||
|
// Lua global "control_rate") to choose optimal behaviour
|
||||||
|
// (task).
|
||||||
|
// ============================================================
|
||||||
|
|
||||||
|
|
||||||
|
// Basic task.
|
||||||
|
// name : Tasks name (Lua function.)
|
||||||
|
// target : Target, this will depend on the task itself.
|
||||||
|
typedef struct {
|
||||||
|
char* name;
|
||||||
|
void* target;
|
||||||
|
} Task;
|
||||||
|
|
||||||
|
// Global Lua interpreter.
|
||||||
static lua_State* L = NULL;
|
static lua_State* L = NULL;
|
||||||
|
|
||||||
int ai_init(void) {
|
int ai_init(void) {
|
||||||
|
28
src/main.c
28
src/main.c
@ -110,7 +110,7 @@ int main(int argc, char** argv) {
|
|||||||
lua_pushstring(L, "key");
|
lua_pushstring(L, "key");
|
||||||
lua_gettable(L, -3);
|
lua_gettable(L, -3);
|
||||||
if(lua_isnumber(L, -1))
|
if(lua_isnumber(L, -1))
|
||||||
str = (int)lua_tonumber(L, -1);
|
key = (int)lua_tonumber(L, -1);
|
||||||
|
|
||||||
// Is it reversed? Only useful for axis.
|
// Is it reversed? Only useful for axis.
|
||||||
lua_pushstring(L, "reverse");
|
lua_pushstring(L, "reverse");
|
||||||
@ -187,9 +187,8 @@ int main(int argc, char** argv) {
|
|||||||
ships_load();
|
ships_load();
|
||||||
|
|
||||||
// Testing.
|
// Testing.
|
||||||
unsigned int player_id;
|
pilot_create(get_ship("Ship"), "Player", NULL, NULL, PILOT_PLAYER);
|
||||||
player_id = pilot_create(get_ship("Ship"), "Player", NULL, NULL, PILOT_PLAYER);
|
gl_bindCamera(&player->solid->pos);
|
||||||
gl_bindCamera(&get_pilot(player_id)->solid->pos);
|
|
||||||
space_init();
|
space_init();
|
||||||
|
|
||||||
pilot_create(get_ship("Miss. Test"), NULL, NULL, NULL, 0);
|
pilot_create(get_ship("Miss. Test"), NULL, NULL, NULL, 0);
|
||||||
@ -225,14 +224,19 @@ int main(int argc, char** argv) {
|
|||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update all the things.
|
// == Update everything. ==================================
|
||||||
// Space:
|
// Blitting order. (layers)
|
||||||
// -- Stars.
|
//
|
||||||
// -- Movement.
|
// BG | Stars and planets.
|
||||||
// -- Render.
|
// | Background particles.
|
||||||
// Pilots:
|
// X
|
||||||
// -- Think (ai).
|
// N | NPC ships.
|
||||||
// -- Solid.
|
// | Normal layer particles (above ships).
|
||||||
|
// X
|
||||||
|
// FG | Player.
|
||||||
|
// | Foreground particles.
|
||||||
|
// | Text and GUI.
|
||||||
|
// ========================================================
|
||||||
static void update_all(void) {
|
static void update_all(void) {
|
||||||
double dt = (double)(SDL_GetTicks() - time) / 1000.0;
|
double dt = (double)(SDL_GetTicks() - time) / 1000.0;
|
||||||
time = SDL_GetTicks();
|
time = SDL_GetTicks();
|
||||||
|
18
src/opengl.c
18
src/opengl.c
@ -24,8 +24,8 @@ Vec2* gl_camera;
|
|||||||
gl_font gl_defFont;
|
gl_font gl_defFont;
|
||||||
|
|
||||||
// Misc.
|
// Misc.
|
||||||
static int _flip_surface(SDL_Surface* surface);
|
static int SDL_VFlipSurface(SDL_Surface* surface);
|
||||||
static int _pot(int n);
|
static int pot(int n);
|
||||||
// gl_texture.
|
// gl_texture.
|
||||||
static GLuint gl_loadSurface(SDL_Surface* surface, int* rw, int* rh);
|
static GLuint gl_loadSurface(SDL_Surface* surface, int* rw, int* rh);
|
||||||
// Gl font.
|
// Gl font.
|
||||||
@ -36,7 +36,7 @@ static void gl_fontMakeDList(FT_Face face, char ch, GLuint list_base, GLuint* te
|
|||||||
// ================
|
// ================
|
||||||
|
|
||||||
// Get me the closest power of two plox.
|
// Get me the closest power of two plox.
|
||||||
static int _pot(int n) {
|
static int pot(int n) {
|
||||||
int i = 1;
|
int i = 1;
|
||||||
while(i < n)
|
while(i < n)
|
||||||
i<<=1;
|
i<<=1;
|
||||||
@ -44,7 +44,7 @@ static int _pot(int n) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Flips the surface vertically. Return 0 on success.
|
// Flips the surface vertically. Return 0 on success.
|
||||||
static int _flip_surface(SDL_Surface* surface) {
|
static int SDL_VFlipSurface(SDL_Surface* surface) {
|
||||||
// Flip the image.
|
// Flip the image.
|
||||||
Uint8* rowhi, *rowlo, *tmpbuf;
|
Uint8* rowhi, *rowlo, *tmpbuf;
|
||||||
int y;
|
int y;
|
||||||
@ -83,8 +83,8 @@ static GLuint gl_loadSurface(SDL_Surface* surface, int* rw, int* rh) {
|
|||||||
int potw, poth;
|
int potw, poth;
|
||||||
|
|
||||||
// Make size power of two.
|
// Make size power of two.
|
||||||
potw = _pot(surface->w);
|
potw = pot(surface->w);
|
||||||
poth = _pot(surface->h);
|
poth = pot(surface->h);
|
||||||
if(rw)*rw = potw;
|
if(rw)*rw = potw;
|
||||||
if(rh)*rh = poth;
|
if(rh)*rh = poth;
|
||||||
|
|
||||||
@ -201,7 +201,7 @@ gl_texture* gl_newImage(const char* path) {
|
|||||||
|
|
||||||
SDL_FreeSurface(tmp); // Free the temp surface.
|
SDL_FreeSurface(tmp); // Free the temp surface.
|
||||||
|
|
||||||
if(_flip_surface(surface)) {
|
if(SDL_VFlipSurface(surface)) {
|
||||||
WARN("Error flipping surface");
|
WARN("Error flipping surface");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -359,8 +359,8 @@ static void gl_fontMakeDList(FT_Face face, char ch, GLuint list_base, GLuint* te
|
|||||||
bitmap = bitmap_glyph->bitmap; // To simplify.
|
bitmap = bitmap_glyph->bitmap; // To simplify.
|
||||||
|
|
||||||
// Need the POT wrapping for GL.
|
// Need the POT wrapping for GL.
|
||||||
w = _pot(bitmap.width);
|
w = pot(bitmap.width);
|
||||||
h = _pot(bitmap.rows);
|
h = pot(bitmap.rows);
|
||||||
|
|
||||||
// Memory for textured data.
|
// Memory for textured data.
|
||||||
// Bitmap is useing two channels, one for luminosity and one for alpha.
|
// Bitmap is useing two channels, one for luminosity and one for alpha.
|
||||||
|
@ -7,6 +7,9 @@
|
|||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "pilot.h"
|
#include "pilot.h"
|
||||||
|
|
||||||
|
#define VMOD(v) (v.x*v.x + v.y*v.y)
|
||||||
|
#define NMOD(n) (n*n)
|
||||||
|
|
||||||
// Stack of pilot id's to assure uniqueness.
|
// Stack of pilot id's to assure uniqueness.
|
||||||
static unsigned int pilot_id = 0;
|
static unsigned int pilot_id = 0;
|
||||||
|
|
||||||
@ -54,6 +57,10 @@ static void pilot_update(Pilot* pilot, const double dt) {
|
|||||||
// Update the solid.
|
// Update the solid.
|
||||||
pilot->solid->update(pilot->solid, dt);
|
pilot->solid->update(pilot->solid, dt);
|
||||||
|
|
||||||
|
if(VMOD(pilot->solid->vel) > NMOD(pilot->ship->speed)) {
|
||||||
|
// Should not go faster.
|
||||||
|
}
|
||||||
|
|
||||||
pilot_render(pilot);
|
pilot_render(pilot);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,7 +75,6 @@ void pilot_init(Pilot* pilot, Ship* ship, char* name, const Vec2* vel, const Vec
|
|||||||
pilot->id = ++pilot_id; // New unique pilot id based on pilot_id, Can't be 0.
|
pilot->id = ++pilot_id; // New unique pilot id based on pilot_id, Can't be 0.
|
||||||
|
|
||||||
pilot->ship = ship;
|
pilot->ship = ship;
|
||||||
|
|
||||||
pilot->name = strdup((name == NULL) ? ship->name:name);
|
pilot->name = strdup((name == NULL) ? ship->name:name);
|
||||||
|
|
||||||
pilot->solid = solid_create(ship->mass, vel, pos);
|
pilot->solid = solid_create(ship->mass, vel, pos);
|
||||||
|
@ -10,6 +10,6 @@ void player_rmFlag(unsigned int flag);
|
|||||||
// Input.
|
// Input.
|
||||||
void input_init(void);
|
void input_init(void);
|
||||||
void input_exit(void);
|
void input_exit(void);
|
||||||
void input_set_Keybind(char* keybind, KeybindType type, int key, int reverse);
|
void input_setKeybind(char* keybind, KeybindType type, int key, int reverse);
|
||||||
void input_handle(SDL_Event* event);
|
void input_handle(SDL_Event* event);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user