Right before bringing in the code to render sprites, but after I restructured to have the sprite data.
This commit is contained in:
parent
0828fb584e
commit
93b7faa369
2 changed files with 88 additions and 59 deletions
|
@ -412,44 +412,44 @@ int main(int /*argc*/, char */*argv*/[])
|
||||||
//speed modifiers
|
//speed modifiers
|
||||||
double moveSpeed = frameTime * 3.0; //the constant value is in squares/second
|
double moveSpeed = frameTime * 3.0; //the constant value is in squares/second
|
||||||
double rotSpeed = frameTime * 2.0; //the constant value is in radians/second
|
double rotSpeed = frameTime * 2.0; //the constant value is in radians/second
|
||||||
|
readKeys();
|
||||||
SDL_Event event;
|
//move forward if no wall in front of you
|
||||||
while(SDL_PollEvent(&event)) {
|
if (keyDown(SDLK_UP))
|
||||||
if(event.type != SDL_KEYDOWN) continue;
|
{
|
||||||
//move forward if no wall in front of you
|
if(worldMap[int(posX + dirX * moveSpeed)][int(posY)] == false) posX += dirX * moveSpeed;
|
||||||
if(event.key.keysym.sym == SDLK_UP)
|
if(worldMap[int(posX)][int(posY + dirY * moveSpeed)] == false) posY += dirY * moveSpeed;
|
||||||
{
|
}
|
||||||
if(worldMap[int(posX + dirX * moveSpeed)][int(posY)] == false) posX += dirX * moveSpeed;
|
//move backwards if no wall behind you
|
||||||
if(worldMap[int(posX)][int(posY + dirY * moveSpeed)] == false) posY += dirY * moveSpeed;
|
if(keyDown(SDLK_DOWN))
|
||||||
}
|
{
|
||||||
//move backwards if no wall behind you
|
if(worldMap[int(posX - dirX * moveSpeed)][int(posY)] == false) posX -= dirX * moveSpeed;
|
||||||
if(event.key.keysym.sym == SDLK_DOWN)
|
if(worldMap[int(posX)][int(posY - dirY * moveSpeed)] == false) posY -= dirY * moveSpeed;
|
||||||
{
|
}
|
||||||
if(worldMap[int(posX - dirX * moveSpeed)][int(posY)] == false) posX -= dirX * moveSpeed;
|
//rotate to the right
|
||||||
if(worldMap[int(posX)][int(posY - dirY * moveSpeed)] == false) posY -= dirY * moveSpeed;
|
if(keyDown(SDLK_RIGHT))
|
||||||
}
|
{
|
||||||
//rotate to the right
|
//both camera direction and camera plane must be rotated
|
||||||
if(event.key.keysym.sym == SDLK_RIGHT)
|
double oldDirX = dirX;
|
||||||
{
|
dirX = dirX * cos(-rotSpeed) - dirY * sin(-rotSpeed);
|
||||||
//both camera direction and camera plane must be rotated
|
dirY = oldDirX * sin(-rotSpeed) + dirY * cos(-rotSpeed);
|
||||||
double oldDirX = dirX;
|
double oldPlaneX = planeX;
|
||||||
dirX = dirX * cos(-rotSpeed) - dirY * sin(-rotSpeed);
|
planeX = planeX * cos(-rotSpeed) - planeY * sin(-rotSpeed);
|
||||||
dirY = oldDirX * sin(-rotSpeed) + dirY * cos(-rotSpeed);
|
planeY = oldPlaneX * sin(-rotSpeed) + planeY * cos(-rotSpeed);
|
||||||
double oldPlaneX = planeX;
|
}
|
||||||
planeX = planeX * cos(-rotSpeed) - planeY * sin(-rotSpeed);
|
//rotate to the left
|
||||||
planeY = oldPlaneX * sin(-rotSpeed) + planeY * cos(-rotSpeed);
|
if(keyDown(SDLK_LEFT))
|
||||||
}
|
{
|
||||||
//rotate to the left
|
//both camera direction and camera plane must be rotated
|
||||||
if(event.key.keysym.sym == SDLK_LEFT)
|
double oldDirX = dirX;
|
||||||
{
|
dirX = dirX * cos(rotSpeed) - dirY * sin(rotSpeed);
|
||||||
//both camera direction and camera plane must be rotated
|
dirY = oldDirX * sin(rotSpeed) + dirY * cos(rotSpeed);
|
||||||
double oldDirX = dirX;
|
double oldPlaneX = planeX;
|
||||||
dirX = dirX * cos(rotSpeed) - dirY * sin(rotSpeed);
|
planeX = planeX * cos(rotSpeed) - planeY * sin(rotSpeed);
|
||||||
dirY = oldDirX * sin(rotSpeed) + dirY * cos(rotSpeed);
|
planeY = oldPlaneX * sin(rotSpeed) + planeY * cos(rotSpeed);
|
||||||
double oldPlaneX = planeX;
|
}
|
||||||
planeX = planeX * cos(rotSpeed) - planeY * sin(rotSpeed);
|
if(keyDown(SDLK_ESCAPE))
|
||||||
planeY = oldPlaneX * sin(rotSpeed) + planeY * cos(rotSpeed);
|
{
|
||||||
}
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,27 @@
|
||||||
using matrix::Matrix;
|
using matrix::Matrix;
|
||||||
using namespace fmt;
|
using namespace fmt;
|
||||||
|
|
||||||
|
#define texWidth 64 // must be power of two
|
||||||
|
#define texHeight 64 // must be power of two
|
||||||
|
|
||||||
|
|
||||||
|
#define numSprites 1
|
||||||
|
#define numTextures 11
|
||||||
|
|
||||||
|
struct Sprite {
|
||||||
|
double x;
|
||||||
|
double y;
|
||||||
|
int texture;
|
||||||
|
};
|
||||||
|
|
||||||
|
const int RAY_VIEW_WIDTH=1280;
|
||||||
|
const int RAY_VIEW_HEIGHT=720;
|
||||||
|
const int RAY_VIEW_X=0;
|
||||||
|
const int RAY_VIEW_Y=0;
|
||||||
|
|
||||||
|
const int SCREEN_HEIGHT=RAY_VIEW_HEIGHT;
|
||||||
|
const int SCREEN_WIDTH=1280;
|
||||||
|
|
||||||
Matrix MAP{
|
Matrix MAP{
|
||||||
{8,8,8,8,8,8,8,8,8},
|
{8,8,8,8,8,8,8,8,8},
|
||||||
{8,0,2,0,0,0,0,0,8},
|
{8,0,2,0,0,0,0,0,8},
|
||||||
|
@ -23,14 +44,6 @@ Matrix MAP{
|
||||||
{8,8,8,8,8,8,8,8,8}
|
{8,8,8,8,8,8,8,8,8}
|
||||||
};
|
};
|
||||||
|
|
||||||
const int RAY_VIEW_WIDTH=1920;
|
|
||||||
const int RAY_VIEW_HEIGHT=1080;
|
|
||||||
const int RAY_VIEW_X=0;
|
|
||||||
const int RAY_VIEW_Y=0;
|
|
||||||
|
|
||||||
const int SCREEN_HEIGHT=RAY_VIEW_HEIGHT;
|
|
||||||
const int SCREEN_WIDTH=1920;
|
|
||||||
|
|
||||||
const int MAP_SIZE=matrix::width(MAP);
|
const int MAP_SIZE=matrix::width(MAP);
|
||||||
const int TILE_SIZE=RAY_VIEW_HEIGHT / MAP_SIZE;
|
const int TILE_SIZE=RAY_VIEW_HEIGHT / MAP_SIZE;
|
||||||
int PITCH=0;
|
int PITCH=0;
|
||||||
|
@ -56,17 +69,24 @@ double planeY = 0.66;
|
||||||
#define rgba_color(r,g,b,a) (r<<(0*8))|(g<<(1*8))|(b<<(2*8))|(a<<(3*8))
|
#define rgba_color(r,g,b,a) (r<<(0*8))|(g<<(1*8))|(b<<(2*8))|(a<<(3*8))
|
||||||
#define gray_color(c) rgba_color(c, c, c, 255)
|
#define gray_color(c) rgba_color(c, c, c, 255)
|
||||||
|
|
||||||
std::vector<uint32_t> texture[8];
|
Sprite sprites[numSprites] = {
|
||||||
#define texWidth 256 // must be power of two
|
{20.5, 11.5, 8}
|
||||||
#define texHeight 256 // must be power of two
|
};
|
||||||
|
|
||||||
|
double ZBuffer[RAY_VIEW_WIDTH];
|
||||||
|
int spriteOrder[numSprites];
|
||||||
|
double spriteDistance[numSprites];
|
||||||
|
|
||||||
|
std::vector<uint32_t> texture[numTextures];
|
||||||
|
|
||||||
#define pixcoord(X, Y) ((Y) * RAY_VIEW_WIDTH) + (X)
|
#define pixcoord(X, Y) ((Y) * RAY_VIEW_WIDTH) + (X)
|
||||||
|
|
||||||
uint32_t pixels[RAY_VIEW_WIDTH * RAY_VIEW_HEIGHT] = {0};
|
uint32_t pixels[RAY_VIEW_WIDTH * RAY_VIEW_HEIGHT] = {0};
|
||||||
|
|
||||||
sf::Texture view_texture;
|
sf::Texture view_texture;
|
||||||
sf::Sprite view_sprite;
|
sf::Sprite view_sprite;
|
||||||
|
|
||||||
|
void sortSprites(int *order, double *dist, int amount);
|
||||||
|
|
||||||
void loadImage(std::vector<uint32_t>& texture, const char *filename) {
|
void loadImage(std::vector<uint32_t>& texture, const char *filename) {
|
||||||
sf::Image img;
|
sf::Image img;
|
||||||
bool good = img.loadFromFile(filename);
|
bool good = img.loadFromFile(filename);
|
||||||
|
@ -76,18 +96,22 @@ void loadImage(std::vector<uint32_t>& texture, const char *filename) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void load_textures() {
|
void load_textures() {
|
||||||
for(int i = 0; i < 8; i++) {
|
for(int i = 0; i < numTextures; i++) {
|
||||||
texture[i].resize(texWidth * texHeight);
|
texture[i].resize(texWidth * texHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
loadImage(texture[0], "pics/tile16.png");
|
loadImage(texture[0], "pics/eagle.png");
|
||||||
loadImage(texture[1], "pics/tile02.png");
|
loadImage(texture[1], "pics/redbrick.png");
|
||||||
loadImage(texture[2], "pics/tile03.png");
|
loadImage(texture[2], "pics/purplestone.png");
|
||||||
loadImage(texture[3], "pics/tile32.png");
|
loadImage(texture[3], "pics/greystone.png");
|
||||||
loadImage(texture[4], "pics/tile05.png");
|
loadImage(texture[4], "pics/bluestone.png");
|
||||||
loadImage(texture[5], "pics/tile17.png");
|
loadImage(texture[5], "pics/mossy.png");
|
||||||
loadImage(texture[6], "pics/tile10.png");
|
loadImage(texture[6], "pics/wood.png");
|
||||||
loadImage(texture[7], "pics/tile01.png");
|
loadImage(texture[7], "pics/colorstone.png");
|
||||||
|
|
||||||
|
loadImage(texture[8], "pics/barrel.png");
|
||||||
|
loadImage(texture[9], "pics/pillar.png");
|
||||||
|
loadImage(texture[10], "pics/greenlight.png");
|
||||||
}
|
}
|
||||||
|
|
||||||
void draw_sfml_rect(sf::RenderWindow &window, sf::Vector2f pos, sf::Vector2f size, uint8_t color) {
|
void draw_sfml_rect(sf::RenderWindow &window, sf::Vector2f pos, sf::Vector2f size, uint8_t color) {
|
||||||
|
@ -412,3 +436,8 @@ int main() {
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void sortSprites(int* order, double* dist, int amount)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue