Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*--HEADERS--*/
- #include <stdio.h>
- #include <stdlib.h>
- #include <GL/glut.h>
- #include <math.h>
- /*--DEFINITIONS--*/
- #define PI 3.141592653589793238
- #define DR 0.0174533 //this is one degree in radians
- struct BUTTON_KEYS
- {
- int w, a, s, d; //tracks button states as 0 (off) or 1 (on);
- };
- int Contains(int list[], int size, int number)
- {
- if(size == -1)
- {
- //Why divide the sizeof the list to the sizeof the first element? Sizeof returns the size in bytes. This means that
- // we can divide the number of bytes in the first element by the size of the whole array to get the array size
- size = sizeof(list) / sizeof(list[0]);
- }
- int i; //Delcaring variables in forloops only works in C99 version of C i guess.
- for(i = 0; i <= size; i++)
- {
- if(list[i] == number)
- {
- return 1;
- }
- }
- return 0;
- }
- int textures[] = //an array numbers. each texture is 1024 bits as they are 32 x 32 textures
- {
- //brown brick texture - 1
- 3, 3, 3, 3, 3, 3, 3, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 3, 3, 3, 3, 3, 3, 3,
- 1, 1, 1, 1, 1, 1, 2, 4, 4, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 4, 4, 3, 3, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 2, 4, 4, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 4, 4, 3, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 2, 4, 4, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 4, 4, 3, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 2, 4, 4, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 4, 4, 3, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 2, 2, 4, 4, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2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 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
- 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
- 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
- 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
- 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
- 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
- 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
- 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
- };
- struct BUTTON_KEYS Keys;
- struct F_TUPLE
- {
- float x;
- float y;
- float z;
- };
- float Convert255to1(float number)
- {
- number = number / 255.0000;
- return number;
- }
- /*--MAP--*/
- int map_size_x = 8, map_size_y = 8, tile_size = 64.0000; //defines the map as being 8 tiles horizontally and vertically, with each tile being 64 units
- //What tile codes are considered doors.
- int DoorTiles[] = {3, 2};
- /*---
- -----
- Wall Map Codes
- 1 - Brown Brick Texture 1 - WALL
- 2. Wood Door Texture 1 - DOOR
- -----
- ---*/
- int wall_map[] =
- {
- 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 0, 0, 0, 0, 0, 0, 1,
- 1, 0, 0, 0, 0, 0, 0, 1,
- 1, 0, 0, 0, 0, 0, 0, 1,
- 1, 0, 0, 0, 0, 0, 0, 1,
- 1, 0, 0, 0, 0, 0, 0, 1,
- 1, 0, 0, 0, 0, 0, 0, 1,
- 1, 1, 1, 1, 1, 1, 1, 1,
- };
- /*---
- -----
- Floor Map Codes - Same as wall map, -1! So brick is 0, wood door is 1, etc.
- -----
- ---*/
- int floor_map[] =
- {
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- };
- int cieling_map[] =
- {
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- };
- void DrawMap2D()
- {
- int x, y; //for loop iterators for each tile, x and y, on t he mpa
- int x_offset, y_offset;
- for(y = 0; y < map_size_y; y++) //for loop continues for each tile on the y axis
- {
- for(x = 0; x < map_size_x; x++) //for loops goes through each x axis tile in each y column
- {
- if(wall_map[y * map_size_x + x] > 0) //a genius way of iterating through a 1 dimensional array as a 2 dimensional matrix. Hard to explain.
- {
- glColor3f(0.9686, 0.7373, 0.6784); //if there's a wall, set the GL color to white
- }
- else
- {
- glColor3f(0.3725, 0.3647, 0.4941); //if there's not a wall, set the GL color to black
- }
- /*
- To draw a 64 unit block, multiply the current x and y coords for that tile by the tile size, 64.
- This means that tile one will be drawn from units 0, 0; to units 64, 64;
- Tile two will be drawn from units 64, 64 to units 128, 128; etc. forming the grid.
- */
- x_offset = x * tile_size; y_offset = y * tile_size; //multiplies x and y by tile size to get the offset required
- glBegin(GL_QUADS); //begins GL in quadrilateral mode
- glVertex2i(x_offset+1, y_offset+1 ); //draws point one at the offset (so for tile zero it will be (0, 0))
- glVertex2i(x_offset+1, y_offset + tile_size-1); //draws point two at the offset plus tile size for y (so for tile zero it will be (0, 64))
- glVertex2i(x_offset-1 + tile_size, y_offset + tile_size-1); //draws point three at the offset plus tile size for both x and y (so for tile zero it will be (64, 64))
- glVertex2i(x_offset-1 + tile_size, y_offset+1 ); //draws point four at the offset plus tile size for x (so for tile zero it will be (64, 0))
- glEnd();
- /*
- This creates a quadrilateral with points (0, 0)----------(0, 64) for tile zero
- | |
- | |
- | |
- (64, 0)----------(64, 64)
- This creates a quadrilateral with points (64, 64)----------(64, 128) for tile one
- | |
- | |
- | |
- (128, 64)----------(128, 128)
- and so on;
- These quads will be drawn regardless of if they are walls or not. If they are not walls, they will be
- drawn as black instead of white, but they are still drawn;
- The plus and minus one throughout is just to draw a border around the blocks for visibility
- */
- }
- }
- }
- /*--PLAYER--*/
- float player_x, player_y; //player position
- float player_delta_x, player_delta_y; //stores the amount of movement in the horizontal/vertical direction the player should move
- float player_angle;
- void DrawPlayer2D()
- {
- glColor3f(0.1765, 0.2588, 0.3216); //sets the current gl color
- glPointSize(8); //sets the point size to 8
- glBegin(GL_POINTS); //starts GL in point drawing mode
- glVertex2i(player_x, player_y); //uses the vertex integer draw function to draw a point at player's position
- glEnd(); //ends the current GL process
- glLineWidth(3);
- glBegin(GL_LINES);
- //draw a line from the players position to the players delta position, i.e. where they are facing
- glVertex2i(player_x, player_y);
- glVertex2i(player_x + player_delta_x * 5, player_y + player_delta_y * 5);
- glEnd();
- }
- /*---RAYS---*/
- float Dist(float ax, float ay, float bx, float by, float ang)
- {
- float horizontal_dist = bx - ax;
- float vertical_dist = by - ay;
- float horizontal_dist_sqr = horizontal_dist * horizontal_dist;
- float vertical_dist_sqr = vertical_dist * vertical_dist;
- float final_dist = sqrt(horizontal_dist_sqr + vertical_dist_sqr);
- return final_dist;
- }
- void DrawRays2D()
- {
- int r, mx, my, mp, depth_of_field; //DoF == number of tiles to cast over, if more than DoF then just end the ray
- float ray_x, ray_y, ray_angle, x_offset, y_offset;
- float final_distance; //holds either horizontal or vertical distance, whicherver is shorter
- float horizontal_distance = 100000;
- float horizontal_x = player_x;
- float horizontal_y = player_y;
- ray_angle = player_angle - (DR * 30);
- if(ray_angle < 0)
- {
- ray_angle += 2*PI;
- }
- if(ray_angle > 2*PI)
- {
- ray_angle -= 2*PI;
- }
- for(r = 0; r < 240; r++)
- {
- int vmt = 0, hmt = 0; //vertical and horizontal map texture code
- // Check Horizontal Lines
- depth_of_field = 0;
- float aTan = -1 / tan(ray_angle);
- if(ray_angle > PI) //if the ray's angle is greater than Pi aka 180 degs, the player is looking up;
- {
- ray_y = ((((int)player_y) / tile_size) * tile_size) - 0.0001; //rounds the ray's y value to the nearest tile. Because each tile is 64 units, dividing and then multiplying by 64 rounds to the nearest multiple of 64, ensuring that the max y distance per depth_of_field iteration is a multiple of 64
- //the -0.0001 is for accuracy. Floating point numbers are inherently a bit inaccurate, meaning sometimes the rays will overshoot their collision and keep firing forward to the next wall.
- //Subtracting 0.0001 ensures that all rays undershoot by just a small amount, not enough to be perceptible but enough to prevent overshooting.
- //Overshooting causes the ray to go to the next wall, which is 64 blocks away. Undershooting by 0.0001 is better than overshooting by 64.
- /*---
- -----
- Tangent (Tan) is opposite over adjacent. We know the opposite line (the vertical one) is player_y - ray_y.
- The Inverse Tangent (aTan) is adjacent over opposite. By multiplying opposite by aTan, we are multiplying
- opp * adj/opp, the opps cancel out and give us adj. We then add the player's x position to adj to figure out
- where on the map the ray's x vector is.
- -----
- ---*/
- ray_x = (player_y - ray_y) * aTan + player_x;
- /*---
- -----
- the offsets calculate the next hit position. y is the same, just up by 64 (GL renders with 0, 0 being top left,
- and moving down increases y position). x is the negative of the y_offset (because x is rendered normally,
- left to right. If we shoot a ray and don't hit, we increment the ray's y value by subtracting 64, then use
- the aTan trick to find the proper x offset.
- -----
- ---*/
- y_offset = -64;
- x_offset = -y_offset * aTan;
- }
- if(ray_angle < PI) //if the ray's angle is less than Pi aka 180 degs, the player is looking down;
- {
- ray_y = ((((int)player_y) / tile_size) * tile_size) + tile_size;
- //rounds the ray's y value to the nearest tile. Because each tile is 64 units, dividing and then multiplying
- //by 64 rounds to the nearest multiple of 64, ensuring that the max y distance per depth_of_field iteration is a multiple
- //of 64. By adding the tile_size (64), we ensure that the ray stops at the top of the tile rather than the bottom
- ray_x = (player_y - ray_y) * aTan + player_x;
- y_offset = 64;
- x_offset = -y_offset * aTan;
- }
- if(ray_angle == 0 || ray_angle == PI) //player is looking straight left or right, the ray will never intersect
- {
- ray_x = player_x;
- ray_y = player_y;
- depth_of_field = 16; //Setting depth_of_field to 8 means that we don't need to continue casting rays
- }
- while(depth_of_field < 16) //if depth_of_field is less than 8, keep casting until it hits something or iterates 8 times
- {
- mx = (int)(ray_x) / tile_size; //the ray's x value divided by 64, effectively giving the tile position's x value
- my = (int)(ray_y) / tile_size; //the ray's y value divided by 64, effectively giving the tile position's y value
- mp = my * map_size_x + mx; //The ray's final point on the map
- if(mp > 0 && mp < map_size_x * map_size_y && wall_map[mp] > 0) //if the ray's final point on the map is less than the size of the map (AKA not out of bounds) and is a 1, then the ray hit a wall. Set depth_of_field to 8 so that the while loop stops. The search is over.
- {
- hmt = wall_map[mp]-1;
- horizontal_x = ray_x;
- horizontal_y = ray_y;
- horizontal_distance = Dist(player_x, player_y, horizontal_x, horizontal_y, ray_angle);
- depth_of_field = 16;
- }
- else
- {
- ray_x += x_offset;
- ray_y += y_offset;
- depth_of_field += 1;
- }
- }
- // Check Vertical Lines
- depth_of_field = 0;
- float nTan = -tan(ray_angle);
- float vertical_distance = 100000;
- float vertical_x = player_x;
- float vertical_y = player_y;
- if(ray_angle > (PI/2) && ray_angle < ((3*PI)/2)) //if the ray's angle is greater than PI/2 aka 90 degs, and less than 3PI/2 aka 270 degs, the player is looking left;
- {
- ray_x = ((((int)player_x) / tile_size) * tile_size) - 0.0001;
- ray_y = (player_x - ray_x) * nTan + player_y;
- x_offset = -64;
- y_offset = -x_offset * nTan;
- }
- if(ray_angle < (PI/2) || ray_angle > ((3*PI)/2)) //if the ray's angle is less than PI/2 aka 90 or greater than 3PI/2 aka 270 degs, the player is looking right;
- {
- ray_x = ((((int)player_x) / tile_size) * tile_size) + tile_size;
- ray_y = (player_x - ray_x) * nTan + player_y;
- x_offset = 64;
- y_offset = -x_offset * nTan;
- }
- if(ray_angle == 0 || ray_angle == PI) //player is looking straight up or down, the ray will never intersect
- {
- ray_x = player_x;
- ray_y = player_y;
- depth_of_field = 16;
- }
- while(depth_of_field < 16)
- {
- mx = (int)(ray_x) / tile_size;
- my = (int)(ray_y) / tile_size;
- mp = my * map_size_x + mx;
- if(mp > 0 && mp < map_size_x * map_size_y && wall_map[mp] > 0)
- {
- vmt = wall_map[mp]-1;
- vertical_x = ray_x;
- vertical_y = ray_y;
- vertical_distance = Dist(player_x, player_y, vertical_x, vertical_y, ray_angle);
- depth_of_field = 16;
- }
- else
- {
- ray_x += x_offset;
- ray_y += y_offset;
- depth_of_field += 1;
- }
- }
- float shade = 1; //value to shade our textures with
- if(horizontal_distance < vertical_distance)
- {
- ray_x = horizontal_x;
- ray_y = horizontal_y;
- final_distance = horizontal_distance;
- }
- if(horizontal_distance > vertical_distance)
- {
- hmt = vmt;
- ray_x = vertical_x;
- ray_y = vertical_y;
- final_distance = vertical_distance;
- }
- //printf("Horizontal X: %f\nHorizontal Y: %f\nHorizontal Dist: %f\n\nVertical X: %f\nVertical Y: %f\nVertical Dist: %f\n\nRay X: %f\n Ray Y: %f\nRayDist: &f\n\n", horizontal_x, horizontal_y, horizontal_distance, vertical_x, vertical_y, vertical_distance, ray_x, ray_y, (float)Dist(player_x, player_y, ray_x, ray_y, ray_angle));
- glColor3f (0.6706, 0.5098, 0.6000);
- glLineWidth(3);
- glBegin(GL_LINES);
- glVertex2i(player_x, player_y);
- glVertex2i(ray_x, ray_y);
- glEnd();
- /*------------*\
- ----------------
- DRAWING 3D WALLS!
- ----------------
- \*------------*/
- float angle_offset = player_angle - ray_angle; //finds the distance between the player_angle (forward) and each ray
- if(angle_offset < 0)
- {
- angle_offset += 2*PI;
- }
- if(angle_offset > 2*PI)
- {
- angle_offset -= 2*PI;
- }
- final_distance = final_distance * cos(angle_offset); //finds the correct ratio needed for level walls
- float screen_height = 320; float screen_width = 530;
- int line_height = (tile_size * screen_height) / final_distance; //By dividing by final distance, further away things will have a higher final distance and thus will divide more, creating a smaller object
- float texture_y_step = 32.0 / (float)line_height; //because there are 32 bits in each y texture, we divide the line_height by 32 to map over the textures to different line heights
- float texture_y_offset = 0;
- //Notes: we need to do the texture_y_step calc before clamping the line_height between 0 and 320. This is to ensure that textures are drawn to the full line, even if that line is later clipped out of frame
- if(line_height > screen_height)
- {
- texture_y_offset = (line_height - screen_height) / 2.0;
- line_height = screen_height; //ensures that we are not drawing lines offscreen;
- }
- float line_offset = (screen_height/2) - (line_height/2); //lowers the y value of each line so that it is centered on screen
- /*---
- -----
- Draws a line every 8 pixels (vertex x value is r iteration times 8) with a width of 8. It is then shifted
- 530 pixels to the right so that it is not overlapping with the 2D scene;
- All raycast 3D games have a fisheye warping effect. This is because lines further away from the center ray must
- travel further than the center line, making their final_distance larger and thus the line smaller. To fix this, we
- can calculate the distance between the center ray and each off-angle ray, then use trigonometry to find the correct
- ratio to make all walls level;
- -----
- ---*/
- int y;
- float texture_y = texture_y_offset * texture_y_step + (hmt * 32);
- float texture_x;
- if(horizontal_distance < vertical_distance)
- {
- texture_x = 31 - (int)(ray_x / 2.0) % 32; //by doing mod 32 on the ray_x, we can effectively loop the ray x value between 0 and 31. This means that as the ray moves right, the texture_x value goes from 0 to 1 to 2 etc. across the textures[]
- if(ray_angle > PI) //if the player is looking south, then flip the texture. Count from 31 to 0 not 0 to 31. This is to ensure consistent texture as textures appear flipped depending on orientation
- {
- texture_x = 31 - texture_x;
- }
- }
- else if(horizontal_distance > vertical_distance)
- {
- texture_x = (int)(ray_y / 2.0) % 32; //by doing mod 32 on the ray_x, we can effectively loop the ray x value between 0 and 31. This means that as the ray moves right, the texture_x value goes from 0 to 1 to 2 etc. across the textures[]
- if(ray_angle > PI/2 && ray_angle < 3*PI/2) //if the player is looking left (more that 90, less that 270 degs), then flip the texture. Count from 31 to 0 not 0 to 31. This is to ensure consistent texture as textures appear flipped depending on orientation
- {
- texture_x = 31 - texture_x;
- }
- }
- //shade = shade * 512 / final_distance / 2; //calculates distance shading
- if(shade < 0.4)
- {
- shade = 0.4;
- }
- else if(shade > 1)
- {
- shade = 1;
- }
- /*---
- -----
- DRAWING WALLS
- -----
- ---*/
- for(y = 0; y < line_height; y++) //draws each pixel of each line in order to draw pixels
- {
- float c = textures[(int)(texture_y) * 32 + (int)(texture_x)]; //finds the texture code
- //COLOR BASED ON TEXTURE CODE
- struct F_TUPLE color;
- if(c == 0) //black
- {
- color.x = Convert255to1(0.0);
- color.y = Convert255to1(0.0);
- color.z = Convert255to1(0.0);
- }
- else if(c == 1) //brick_color
- {
- color.x = 59.0 / 255.0;
- color.y = 19.0 / 255.0;
- color.z = 10.0 / 255.0;
- }
- else if(c == 2) //brick_shaded
- {
- color.x = 41.0 / 255.0;
- color.y = 7.0 / 255.0;
- color.z = 4.0 / 255.0;
- }
- else if(c == 3) //brick_highlight
- {
- color.x = 65.0 / 255.0;
- color.y = 28.0 / 255.0;
- color.z = 9.0 / 255.0;
- }
- else if(c == 4) //brick_mortar
- {
- color.x = 27.0 / 255.0;
- color.y = 4.0 / 255.0;
- color.z = 12.0 / 255.0;
- }
- else if(c == 5) //wood_wall_1_outline
- {
- color.x = 42.0 / 255.0;
- color.y = 24.0 / 255.0;
- color.z = 17.0 / 255.0;
- }
- else if(c == 6) //wood_wall_1_main
- {
- color.x = 60.0 / 255.0;
- color.y = 36.0 / 255.0;
- color.z = 28.0 / 255.0;
- }
- else if(c == 7) //wood_wall_1_highlight
- {
- color.x = 65.0 / 255.0;
- color.y = 41.0 / 255.0;
- color.z = 33.0 / 255.0;
- }
- else if(c == 8) //wood_wall_1_glass
- {
- color.x = 33.0 / 255.0;
- color.y = 67.0 / 255.0;
- color.z = 81.0 / 255.0;
- }
- else if(c == 9) //wood_wall_1_glasslight
- {
- color.x = 41.0 / 255.0;
- color.y = 76.0 / 255.0;
- color.z = 86.0 / 255.0;
- }
- else if(c == 10) //wood_wall_1_handle
- {
- color.x = 75.0 / 255.0;
- color.y = 68.0 / 255.0;
- color.z = 66.0 / 255.0;
- }
- glColor3f(color.x * shade, color.y * shade, color.z * shade);
- glPointSize(2);
- glBegin(GL_POINTS);
- glVertex2i(r*2+530, y + line_offset); //draws a point at the line_offset then adds the y iterant so that each pixel of the line is drawn individually
- glEnd();
- texture_y += texture_y_step; //increases to the next texture y value each pixel, reading data from textures[];
- }
- /*---
- -----
- DRAWING FLOORS
- -----
- ---*/
- //y starts at the middle of the screen due to the line offset, then goes down the size of the line to reach the
- //bottom part of the line, AKA the start of the floor. Then,
- for(y = line_offset + line_height; y < screen_height; y++) //start drawing at the bottom of the line (line_offset) and go draw down from there (positive y = down)
- {
- float dy = y - (screen_height/2.0); //we get the floor line start point by going
- float deg = ray_angle;
- float ra_fix = player_angle - ray_angle;
- if(ra_fix < 0)
- {
- ra_fix += 2*PI;
- }
- if(ra_fix > 2*PI)
- {
- ra_fix -= 2*PI;
- }
- ra_fix = cos(ra_fix);
- texture_x = player_x/2 + cos(deg)*158*32/dy/ra_fix;
- texture_y = player_y/2 + sin(deg)*158*32/dy/ra_fix;
- int mp = floor_map[(int)(texture_y/32.0) * map_size_x + (int)(texture_x/32.0)]*32*32;
- float c = textures[((int)(texture_y)&31)* 32 + ((int)(texture_x)&31)+mp];
- //COLOR BASED ON TEXTURE CODE
- struct F_TUPLE color;
- if(c == 0) //black
- {
- color.x = Convert255to1(0.0);
- color.y = Convert255to1(0.0);
- color.z = Convert255to1(0.0);
- }
- else if(c == 1) //brick_color
- {
- color.x = 59.0 / 255.0;
- color.y = 19.0 / 255.0;
- color.z = 10.0 / 255.0;
- }
- else if(c == 2) //brick_shaded
- {
- color.x = 41.0 / 255.0;
- color.y = 7.0 / 255.0;
- color.z = 4.0 / 255.0;
- }
- else if(c == 3) //brick_highlight
- {
- color.x = 65.0 / 255.0;
- color.y = 28.0 / 255.0;
- color.z = 9.0 / 255.0;
- }
- else if(c == 4) //brick_mortar
- {
- color.x = 27.0 / 255.0;
- color.y = 4.0 / 255.0;
- color.z = 12.0 / 255.0;
- }
- else if(c == 5) //wood_wall_1_outline
- {
- color.x = 42.0 / 255.0;
- color.y = 24.0 / 255.0;
- color.z = 17.0 / 255.0;
- }
- else if(c == 6) //wood_wall_1_main
- {
- color.x = 60.0 / 255.0;
- color.y = 36.0 / 255.0;
- color.z = 28.0 / 255.0;
- }
- else if(c == 7) //wood_wall_1_highlight
- {
- color.x = 65.0 / 255.0;
- color.y = 41.0 / 255.0;
- color.z = 33.0 / 255.0;
- }
- else if(c == 8) //wood_wall_1_glass
- {
- color.x = 33.0 / 255.0;
- color.y = 67.0 / 255.0;
- color.z = 81.0 / 255.0;
- }
- else if(c == 9) //wood_wall_1_glasslight
- {
- color.x = 41.0 / 255.0;
- color.y = 76.0 / 255.0;
- color.z = 86.0 / 255.0;
- }
- else if(c == 10) //wood_wall_1_handle
- {
- color.x = 75.0 / 255.0;
- color.y = 68.0 / 255.0;
- color.z = 66.0 / 255.0;
- }
- glColor3f(color.x * shade, color.y * shade, color.z * shade);
- glPointSize(2);
- glBegin(GL_POINTS);
- glVertex2i(r*2+530, y); //no line_offset because its used in the for loop
- glEnd();
- /*---
- -----
- DRAWING CIELING
- -----
- ---*/
- mp = cieling_map[(int)(texture_y/32.0) * map_size_x + (int)(texture_x/32.0)]*32*32;
- c = textures[((int)(texture_y)&31)* 32 + ((int)(texture_x)&31)+mp];
- //COLOR BASED ON TEXTURE CODE
- if(c == 0) //black
- {
- color.x = Convert255to1(0.0);
- color.y = Convert255to1(0.0);
- color.z = Convert255to1(0.0);
- }
- else if(c == 1) //brick_color
- {
- color.x = 59.0 / 255.0;
- color.y = 19.0 / 255.0;
- color.z = 10.0 / 255.0;
- }
- else if(c == 2) //brick_shaded
- {
- color.x = 41.0 / 255.0;
- color.y = 7.0 / 255.0;
- color.z = 4.0 / 255.0;
- }
- else if(c == 3) //brick_highlight
- {
- color.x = 65.0 / 255.0;
- color.y = 28.0 / 255.0;
- color.z = 9.0 / 255.0;
- }
- else if(c == 4) //brick_mortar
- {
- color.x = 27.0 / 255.0;
- color.y = 4.0 / 255.0;
- color.z = 12.0 / 255.0;
- }
- else if(c == 5) //wood_wall_1_outline
- {
- color.x = 42.0 / 255.0;
- color.y = 24.0 / 255.0;
- color.z = 17.0 / 255.0;
- }
- else if(c == 6) //wood_wall_1_main
- {
- color.x = 60.0 / 255.0;
- color.y = 36.0 / 255.0;
- color.z = 28.0 / 255.0;
- }
- else if(c == 7) //wood_wall_1_highlight
- {
- color.x = 65.0 / 255.0;
- color.y = 41.0 / 255.0;
- color.z = 33.0 / 255.0;
- }
- else if(c == 8) //wood_wall_1_glass
- {
- color.x = 33.0 / 255.0;
- color.y = 67.0 / 255.0;
- color.z = 81.0 / 255.0;
- }
- else if(c == 9) //wood_wall_1_glasslight
- {
- color.x = 41.0 / 255.0;
- color.y = 76.0 / 255.0;
- color.z = 86.0 / 255.0;
- }
- else if(c == 10) //wood_wall_1_handle
- {
- color.x = 75.0 / 255.0;
- color.y = 68.0 / 255.0;
- color.z = 66.0 / 255.0;
- }
- glColor3f(color.x * shade, color.y * shade, color.z * shade);
- glPointSize(2);
- glBegin(GL_POINTS);
- glVertex2i(r*2+530, screen_height-y); //no line_offset because its used in the for loop
- glEnd();
- }
- ray_angle += DR / 4;
- if(ray_angle < 0)
- {
- ray_angle += 2*PI;
- }
- if(ray_angle > 2*PI)
- {
- ray_angle -= 2*PI;
- }
- }
- }
- float frame1, frame2, fps;
- void Display()
- {
- int angle = -(int)(player_angle*57.2957795);
- if(angle < 0)
- {
- angle += 360;
- }
- printf("%d\n", angle);
- //fps
- frame2 = glutGet(GLUT_ELAPSED_TIME);
- fps = (frame2 - frame1);
- frame1 = glutGet(GLUT_ELAPSED_TIME);
- MovePlayer();
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clears the color and depth buffer before drawing
- DrawMap2D(); //map must be drawn before player is drawn to ensure that player is drawn afterwards and appears on top
- DrawPlayer2D();
- DrawRays2D();
- glutSwapBuffers(); //swaps the buffers. One buffer draws, the other buffer displays. The draw buffer draws without showing the user, preventing artifacting and other weird junk
- }
- void MovePlayer() //detects keystrokes, if active, moves player
- {
- /*
- ---------
- If A or D are pressed, rotates the character by a small amount. We are moving by radians, not degrees, so 1 degree movement is 0.02 rads. 0.1 rads is 5.7 degrees
- Rotation works like this: You get the player's angle, then use cosine to determine x movement and sine to determine
- y movement. These functions divide the amount of x and y movement by the hypotenuse of the right triangle made by
- the angle of the player forwards. This is to normalize the value between 0 and 1. So if the player is facing all the
- way to the right and moves forward, the player would move in the x direction by 1 and in the y direction by 0. If the
- player is facing 45 degrees, then they would move 0.5 units up and 0.5 units right. This ensures proper movement
- direction. These numbers (delta_x and delta_y) are then multiplied by 5, which is the 'speed' of the character;
- If W or D are pressed, the delta_y and delta_x variables that we calculated are then applied, moving the character
- in the motion we want;
- ---------
- */
- int x_offset = 0; //amount to the left or right of the player for checking collisions
- int y_offset = 0; //amount up or down from the player for checking collisions
- if(player_delta_x < 0) //if the players delta_x is less than zero, the player is moving left. Change the offset check to the left
- {
- x_offset = -20;
- }
- else //if player is moving right, change offset to the right
- {
- x_offset = 20;
- }
- if(player_delta_y < 0) //if the players delta_y is less than zero, the player is moving down. Change the offset check to down
- {
- y_offset = -20;
- }
- else //if player is moving up, change offset to up
- {
- y_offset = 20;
- }
- int player_grid_pos_x = player_x / 64.0; //finds player x pos in the tile_grid by dividing by tile size
- int grid_pos_x_plus_offset = (player_x + x_offset) / tile_size; //adds the offset
- int grid_pos_x_minus_offset = (player_x - x_offset) / tile_size; //subtracts the offset
- int player_grid_pos_y = player_y / 64.0; //finds player x pos in the tile_grid by dividing by tile size
- int grid_pos_y_plus_offset = (player_y + y_offset) / tile_size; //adds the offset
- int grid_pos_y_minus_offset = (player_y - y_offset) / tile_size; //subtracts the offset
- if(Keys.w == 1)
- {
- if(wall_map[player_grid_pos_y * map_size_x + grid_pos_x_plus_offset]==0) // y * width + x pattern, same as before if the tile in front of player is a zero, move player
- {
- player_x += player_delta_x * 0.2 * fps;
- }
- if(wall_map[grid_pos_y_plus_offset * map_size_x + player_grid_pos_x]==0)
- {
- player_y += player_delta_y * 0.2 * fps;
- }
- }
- if(Keys.a == 1)
- {
- player_angle -= 0.0035 * fps;
- if(player_angle < 0)
- {
- player_angle += 2*PI; //If player angle is negative, add 2*PI to ensure it is always between 0 and 360 degs
- }
- player_delta_x = cos(player_angle) * 0.2 * fps; //like Time.deltaTime, by multiplying the value by the elapsed time, we get frame-independent movement
- player_delta_y = sin(player_angle) * 0.2 * fps;
- }
- if(Keys.s == 1)
- {
- if(wall_map[player_grid_pos_y * map_size_x + grid_pos_x_minus_offset]==0) // exactly like w key, but using the reverse offset
- {
- player_x -= player_delta_x * 0.2 * fps;
- }
- if(wall_map[grid_pos_y_minus_offset * map_size_x + player_grid_pos_x]==0)
- {
- player_y -= player_delta_y * 0.2 * fps;
- }
- }
- if(Keys.d == 1)
- {
- player_angle += 0.0035 * fps;
- if(player_angle > (2*PI))
- {
- player_angle -= 2*PI; //If player angle is above 360 degrees, subtract 2*PI to ensure it is always between 0 and 360 degs
- }
- player_delta_x = cos(player_angle) * 0.2 * fps;
- player_delta_y = sin(player_angle) * 0.2 * fps;
- }
- glutPostRedisplay(); //tells GL that the screen needs to be redrawn because the player moved
- }
- void ButtonsDown(unsigned char key, int x, int y)
- {
- if(key == 'w')
- {
- Keys.w = 1;
- }
- if(key == 'a')
- {
- Keys.a = 1;
- }
- if(key == 's')
- {
- Keys.s = 1;
- }
- if(key == 'd')
- {
- Keys.d = 1;
- }
- if(key == 'e')
- {
- //xo and yo are the offsets to check if a door is present. If it is, open the door
- int x_offset = 0;
- if(player_delta_x < 0)
- {
- x_offset = -25;
- }
- else
- {
- x_offset = 25;
- }
- int y_offset = 0;
- if(player_delta_y < 0)
- {
- y_offset = -25;
- }
- else
- {
- y_offset = 25;
- }
- //We only add the offset, not add and subtract like we did for collisions. This is because collisions need to check
- //All sides. We can only open doors in front of us.
- int player_grid_pos_x = player_x / tile_size; //finds player x pos in the tile_grid by dividing by tile size
- int grid_pos_x_plus_offset = (player_x + x_offset) / tile_size; //adds the offset
- int player_grid_pos_y = player_y / tile_size; //finds player x pos in the tile_grid by dividing by tile size
- int grid_pos_y_plus_offset = (player_y + y_offset) / tile_size; //adds the offset
- //if the player's grid position is a door code, then change it to an empty space
- if(Contains(DoorTiles, -1, wall_map[grid_pos_y_plus_offset * map_size_x + grid_pos_x_plus_offset])==1) // y * width + x pattern, same as before if the tile in front of player is a zero, move player
- {
- wall_map[grid_pos_y_plus_offset * map_size_x + grid_pos_x_plus_offset] = 0;
- }
- }
- //glutPostRedisplay();
- }
- void ButtonsUp(unsigned char key, int x, int y)
- {
- if(key == 'w')
- {
- Keys.w = 0;
- }
- if(key == 'a')
- {
- Keys.a = 0;
- }
- if(key == 's')
- {
- Keys.s = 0;
- }
- if(key == 'd')
- {
- Keys.d = 0;
- }
- //glutPostRedisplay();
- }
- int SCREEN_WIDTH = 1024;
- int SCREEN_HEIGHT = 510;
- void ResizeScreen(int w, int h)
- {
- glutReshapeWindow(SCREEN_WIDTH, SCREEN_HEIGHT);
- }
- void Init()
- {
- glClearColor(0.3725, 0.3647, 0.4941, 0);
- gluOrtho2D(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0);
- player_x = 84; player_y = 84; //sets the player's position
- player_delta_x = cos(player_angle) * 0.5; player_delta_y = sin(player_angle) * 0.5; //sets the delta_x and y to show the line when the program starts
- }
- int main(int argc, char* argv[]) //dont worry about the parameters
- {
- glutInit(&argc, argv); //dont worry about the parameters
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); //sets display mode to double buffer (uses buffer swap) and RGBA
- glutInitWindowSize(SCREEN_WIDTH, SCREEN_HEIGHT);
- glutInitWindowPosition(1920/4, 1080/4);
- glutCreateWindow("My Game");
- Init();
- glutReshapeFunc(ResizeScreen);
- glutDisplayFunc(Display); //the function that displays aka clears, draws, and swaps buffers
- glutKeyboardFunc(ButtonsDown); //this function is called whenever a key is pressed. It calls ButtonsDown with the pressed key as a parameter
- glutKeyboardUpFunc(ButtonsUp); //this function is called whenever a key is pressed. It calls ButtonsUp with the pressed key as a parameter
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement