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,24 +412,21 @@ int main(int /*argc*/, char */*argv*/[])
|
|||
//speed modifiers
|
||||
double moveSpeed = frameTime * 3.0; //the constant value is in squares/second
|
||||
double rotSpeed = frameTime * 2.0; //the constant value is in radians/second
|
||||
|
||||
SDL_Event event;
|
||||
while(SDL_PollEvent(&event)) {
|
||||
if(event.type != SDL_KEYDOWN) continue;
|
||||
readKeys();
|
||||
//move forward if no wall in front of you
|
||||
if(event.key.keysym.sym == SDLK_UP)
|
||||
if (keyDown(SDLK_UP))
|
||||
{
|
||||
if(worldMap[int(posX + dirX * moveSpeed)][int(posY)] == false) posX += dirX * moveSpeed;
|
||||
if(worldMap[int(posX)][int(posY + dirY * moveSpeed)] == false) posY += dirY * moveSpeed;
|
||||
}
|
||||
//move backwards if no wall behind you
|
||||
if(event.key.keysym.sym == SDLK_DOWN)
|
||||
if(keyDown(SDLK_DOWN))
|
||||
{
|
||||
if(worldMap[int(posX - dirX * moveSpeed)][int(posY)] == false) posX -= dirX * moveSpeed;
|
||||
if(worldMap[int(posX)][int(posY - dirY * moveSpeed)] == false) posY -= dirY * moveSpeed;
|
||||
}
|
||||
//rotate to the right
|
||||
if(event.key.keysym.sym == SDLK_RIGHT)
|
||||
if(keyDown(SDLK_RIGHT))
|
||||
{
|
||||
//both camera direction and camera plane must be rotated
|
||||
double oldDirX = dirX;
|
||||
|
@ -440,7 +437,7 @@ int main(int /*argc*/, char */*argv*/[])
|
|||
planeY = oldPlaneX * sin(-rotSpeed) + planeY * cos(-rotSpeed);
|
||||
}
|
||||
//rotate to the left
|
||||
if(event.key.keysym.sym == SDLK_LEFT)
|
||||
if(keyDown(SDLK_LEFT))
|
||||
{
|
||||
//both camera direction and camera plane must be rotated
|
||||
double oldDirX = dirX;
|
||||
|
@ -450,6 +447,9 @@ int main(int /*argc*/, char */*argv*/[])
|
|||
planeX = planeX * cos(rotSpeed) - planeY * sin(rotSpeed);
|
||||
planeY = oldPlaneX * sin(rotSpeed) + planeY * cos(rotSpeed);
|
||||
}
|
||||
if(keyDown(SDLK_ESCAPE))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,6 +11,27 @@
|
|||
using matrix::Matrix;
|
||||
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{
|
||||
{8,8,8,8,8,8,8,8,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}
|
||||
};
|
||||
|
||||
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 TILE_SIZE=RAY_VIEW_HEIGHT / MAP_SIZE;
|
||||
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 gray_color(c) rgba_color(c, c, c, 255)
|
||||
|
||||
std::vector<uint32_t> texture[8];
|
||||
#define texWidth 256 // must be power of two
|
||||
#define texHeight 256 // must be power of two
|
||||
Sprite sprites[numSprites] = {
|
||||
{20.5, 11.5, 8}
|
||||
};
|
||||
|
||||
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)
|
||||
|
||||
uint32_t pixels[RAY_VIEW_WIDTH * RAY_VIEW_HEIGHT] = {0};
|
||||
|
||||
sf::Texture view_texture;
|
||||
sf::Sprite view_sprite;
|
||||
|
||||
void sortSprites(int *order, double *dist, int amount);
|
||||
|
||||
void loadImage(std::vector<uint32_t>& texture, const char *filename) {
|
||||
sf::Image img;
|
||||
bool good = img.loadFromFile(filename);
|
||||
|
@ -76,18 +96,22 @@ void loadImage(std::vector<uint32_t>& texture, const char *filename) {
|
|||
}
|
||||
|
||||
void load_textures() {
|
||||
for(int i = 0; i < 8; i++) {
|
||||
for(int i = 0; i < numTextures; i++) {
|
||||
texture[i].resize(texWidth * texHeight);
|
||||
}
|
||||
|
||||
loadImage(texture[0], "pics/tile16.png");
|
||||
loadImage(texture[1], "pics/tile02.png");
|
||||
loadImage(texture[2], "pics/tile03.png");
|
||||
loadImage(texture[3], "pics/tile32.png");
|
||||
loadImage(texture[4], "pics/tile05.png");
|
||||
loadImage(texture[5], "pics/tile17.png");
|
||||
loadImage(texture[6], "pics/tile10.png");
|
||||
loadImage(texture[7], "pics/tile01.png");
|
||||
loadImage(texture[0], "pics/eagle.png");
|
||||
loadImage(texture[1], "pics/redbrick.png");
|
||||
loadImage(texture[2], "pics/purplestone.png");
|
||||
loadImage(texture[3], "pics/greystone.png");
|
||||
loadImage(texture[4], "pics/bluestone.png");
|
||||
loadImage(texture[5], "pics/mossy.png");
|
||||
loadImage(texture[6], "pics/wood.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) {
|
||||
|
@ -412,3 +436,8 @@ int main() {
|
|||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void sortSprites(int* order, double* dist, int amount)
|
||||
{
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue