[Fix] Just a few optimization.

This commit is contained in:
Allanis 2013-02-02 21:50:13 +00:00
parent 54beb595dc
commit 066d4871cc
5 changed files with 63 additions and 30 deletions

View File

@ -8,6 +8,29 @@
#include "pilot.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;
int ai_init(void) {

View File

@ -62,12 +62,12 @@ int main(int argc, char** argv) {
// input.
input_init();
input_setKeybind("accel", KEYBIND_KEYBOARD, SDLK_UP, 0);
input_setKeybind("accel", KEYBIND_KEYBOARD, SDLK_w, 0);
input_setKeybind("left", KEYBIND_KEYBOARD, SDLK_LEFT, 0);
input_setKeybind("left", KEYBIND_KEYBOARD, SDLK_a, 0);
input_setKeybind("right", KEYBIND_KEYBOARD, SDLK_RIGHT, 0);
input_setKeybind("right", KEYBIND_KEYBOARD, SDLK_d, 0);
input_setKeybind("accel", KEYBIND_KEYBOARD, SDLK_UP, 0);
input_setKeybind("accel", KEYBIND_KEYBOARD, SDLK_w, 0);
input_setKeybind("left", KEYBIND_KEYBOARD, SDLK_LEFT, 0);
input_setKeybind("left", KEYBIND_KEYBOARD, SDLK_a, 0);
input_setKeybind("right", KEYBIND_KEYBOARD, SDLK_RIGHT, 0);
input_setKeybind("right", KEYBIND_KEYBOARD, SDLK_d, 0);
// Use Lua to parse configuration file.
lua_State* L = luaL_newstate();
@ -110,7 +110,7 @@ int main(int argc, char** argv) {
lua_pushstring(L, "key");
lua_gettable(L, -3);
if(lua_isnumber(L, -1))
str = (int)lua_tonumber(L, -1);
key = (int)lua_tonumber(L, -1);
// Is it reversed? Only useful for axis.
lua_pushstring(L, "reverse");
@ -187,9 +187,8 @@ int main(int argc, char** argv) {
ships_load();
// Testing.
unsigned int player_id;
player_id = pilot_create(get_ship("Ship"), "Player", NULL, NULL, PILOT_PLAYER);
gl_bindCamera(&get_pilot(player_id)->solid->pos);
pilot_create(get_ship("Ship"), "Player", NULL, NULL, PILOT_PLAYER);
gl_bindCamera(&player->solid->pos);
space_init();
pilot_create(get_ship("Miss. Test"), NULL, NULL, NULL, 0);
@ -225,14 +224,19 @@ int main(int argc, char** argv) {
exit(EXIT_SUCCESS);
}
// Update all the things.
// Space:
// -- Stars.
// -- Movement.
// -- Render.
// Pilots:
// -- Think (ai).
// -- Solid.
// == Update everything. ==================================
// Blitting order. (layers)
//
// BG | Stars and planets.
// | Background particles.
// X
// N | NPC ships.
// | Normal layer particles (above ships).
// X
// FG | Player.
// | Foreground particles.
// | Text and GUI.
// ========================================================
static void update_all(void) {
double dt = (double)(SDL_GetTicks() - time) / 1000.0;
time = SDL_GetTicks();

View File

@ -24,8 +24,8 @@ Vec2* gl_camera;
gl_font gl_defFont;
// Misc.
static int _flip_surface(SDL_Surface* surface);
static int _pot(int n);
static int SDL_VFlipSurface(SDL_Surface* surface);
static int pot(int n);
// gl_texture.
static GLuint gl_loadSurface(SDL_Surface* surface, int* rw, int* rh);
// 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.
static int _pot(int n) {
static int pot(int n) {
int i = 1;
while(i < n)
i<<=1;
@ -44,7 +44,7 @@ static int _pot(int n) {
}
// 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.
Uint8* rowhi, *rowlo, *tmpbuf;
int y;
@ -83,8 +83,8 @@ static GLuint gl_loadSurface(SDL_Surface* surface, int* rw, int* rh) {
int potw, poth;
// Make size power of two.
potw = _pot(surface->w);
poth = _pot(surface->h);
potw = pot(surface->w);
poth = pot(surface->h);
if(rw)*rw = potw;
if(rh)*rh = poth;
@ -201,7 +201,7 @@ gl_texture* gl_newImage(const char* path) {
SDL_FreeSurface(tmp); // Free the temp surface.
if(_flip_surface(surface)) {
if(SDL_VFlipSurface(surface)) {
WARN("Error flipping surface");
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.
// Need the POT wrapping for GL.
w = _pot(bitmap.width);
h = _pot(bitmap.rows);
w = pot(bitmap.width);
h = pot(bitmap.rows);
// Memory for textured data.
// Bitmap is useing two channels, one for luminosity and one for alpha.

View File

@ -7,6 +7,9 @@
#include "log.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.
static unsigned int pilot_id = 0;
@ -53,6 +56,10 @@ static void pilot_update(Pilot* pilot, const double dt) {
// Update the solid.
pilot->solid->update(pilot->solid, dt);
if(VMOD(pilot->solid->vel) > NMOD(pilot->ship->speed)) {
// Should not go faster.
}
pilot_render(pilot);
}
@ -67,8 +74,7 @@ static void pilot_update(Pilot* pilot, const double dt) {
void pilot_init(Pilot* pilot, Ship* ship, char* name, const Vec2* vel, const Vec2* pos, const int flags) {
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->solid = solid_create(ship->mass, vel, pos);

View File

@ -10,6 +10,6 @@ void player_rmFlag(unsigned int flag);
// Input.
void input_init(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);