#include #include #include #include #include #include #include #include #include using namespace std; // some functions that i copied from a tutorial void DrawPixel(SDL_Surface *screen, int x, int y, Uint8 R, Uint8 G, Uint8 B) { Uint32 color = SDL_MapRGB(screen->format, R, G, B); switch (screen->format->BytesPerPixel) { case 1: // Assuming 8-bpp { Uint8 *bufp; bufp = (Uint8 *)screen->pixels + y*screen->pitch + x; *bufp = color; } break; case 2: // Probably 15-bpp or 16-bpp { Uint16 *bufp; bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x; *bufp = color; } break; case 3: // Slow 24-bpp mode, usually not used { Uint8 *bufp; bufp = (Uint8 *)screen->pixels + y*screen->pitch + x * 3; if(SDL_BYTEORDER == SDL_LIL_ENDIAN) { bufp[0] = color; bufp[1] = color >> 8; bufp[2] = color >> 16; } else { bufp[2] = color; bufp[1] = color >> 8; bufp[0] = color >> 16; } } break; case 4: // Probably 32-bpp { Uint32 *bufp; bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x; *bufp = color; } break; } } void Slock(SDL_Surface *screen) { if ( SDL_MUSTLOCK(screen) ) { if ( SDL_LockSurface(screen) < 0 ) { return; } } } void Sulock(SDL_Surface *screen) { if ( SDL_MUSTLOCK(screen) ) { SDL_UnlockSurface(screen); } } // some player varibles float player_x = 10.5; // 0 to 21 float player_y = 19.5; // 0 to 39 int player_deg_plus_vel = 0; int player_deg_minus_vel = 0; float player_walk_vel= 0; float player_back_walk_vel= 0; int player_height; // this is the height of the players eyes, if you "jump" it goes up, 0 is standard height float player_degrees = 45; // how much the player is turned in degrees int player_speed = 1; // how fast the player moves // some things for the raycasting float slice_distance; // distance of the current slice from the player int distance_counter; // used in finding the distance of the slice int temp_x; // just something used in the engine for converting from float int temp_y; // ditto float current_angle; SDL_Event event; //The SDL event that we will poll to get events. // some declarations const float slice_intensity = 0.1; // used for the shading const float PI = 3.14159265; const float player_view = 60; // degrees of view const float ray_angle = player_view/640; // angle between rays Uint8 r; Uint8 b; Uint8 g; const int mapx = 22; const int mapy = 40; //////////////////////////////////////////////////////////////////////////////// int main(int argc, char *argv[]) { // the map loading stuff char Map[mapx][mapy]; ifstream InFile; InFile.open("raylevel.Map"); for(int i=0;iformat, 0, 0, 0)); Slock(screen); for(int x=0;x<640;x++) { // cast the ray! slice_distance = 0; distance_counter = 0; current_angle = player_degrees - (320 * ray_angle) + (x * ray_angle); for(int counter=0;counter<414;counter++) // 414 is just a random number that will never be reached { temp_x = (int)(player_x + (cos (current_angle * PI/180)* distance_counter)); temp_y = (int)(player_y + (sin (current_angle * PI/180)* distance_counter)); if (temp_x >= 22) { break; } if (temp_x <= -1) { break; } if (temp_y >= 40) { break; } if (temp_y <= -1) { break; } if (Map[temp_x][temp_y] == '*') { r = 200; b = 10; g = 10; break; } if (Map[temp_x][temp_y] == '#') { r = 10; b = 200; g = 10; break; } if (Map[temp_x][temp_y] == '!') { r = 10; b = 10; g = 200; break; } distance_counter++; } int distancevar1; int distancevar2; slice_distance = distance_counter * cos((-30 + (x * ray_angle)) * PI/180); // remove fisheye //if (slice_distance <= 0) slice_distance = 0; distancevar1 = (-60 + slice_distance); distancevar2 = (60 - slice_distance); //cout << for(int y=240 + distancevar1;y<240 + distancevar2;y++) { DrawPixel(screen, x,y,r,b,g); } } Sulock(screen); SDL_Flip(screen); // stole this from the web (with some changes by me) clock_t endwait; endwait = clock () + 0.0016667 * CLOCKS_PER_SEC ; while (clock() < endwait) {} } return 0; }