Skip to content

Commit

Permalink
added gpu rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
mueslimak3r committed Oct 13, 2019
1 parent a146e66 commit eb3a274
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 31 deletions.
33 changes: 26 additions & 7 deletions wolf3d-nintendoswitch/source/draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@ int colors(int i)
return (rgb[i % 62]);
}

uint32_t make_color(int hexValue)
{
uint32_t ret;
ret = hexValue >> 16; // r
ret <<= 16;
ret |= (hexValue & 0x00ff00) >> 8; // g
ret <<= 16;
ret |= (hexValue & 0x0000ff); //b
ret |= 255; // a
/*
rgbColor.r = hexValue >> 16;//((hexValue >> 16) & 0xFF) / 255.0; // Extract the RR byte
rgbColor.g = (hexValue & 0x00ff00) >> 8;//((hexValue >> 8) & 0xFF) / 255.0; // Extract the GG byte
rgbColor.b = (hexValue & 0x0000ff);//((hexValue) & 0xFF) / 255.0; // Extract the BB byte
*/
return (ret);
}

void dda(t_mlx *mlx, t_vect_3 *line, int x, t_ray *ray)
{
int y;
Expand All @@ -33,14 +50,16 @@ void dda(t_mlx *mlx, t_vect_3 *line, int x, t_ray *ray)
else
*/
if (x < mlx->w && x >= 0 && y < mlx->h && y >= 0)
{ //mlx->pixels[mlx->w * y + x] = colors(ray->hit);
t_RGB c = color_converter(colors(ray->hit));
SDL_SetRenderDrawColor(mlx->renderer, c.r, c.g, c.b, 255);
SDL_RenderDrawPoint(mlx->renderer, x, y);
{
mlx->pixels[mlx->w * y + x] = make_color(colors(ray->hit));
//mlx->pixels[mlx->w * y + x] = colors(ray->hit);
//t_RGB c = color_converter(colors(ray->hit));
//SDL_SetRenderDrawColor(mlx->renderer, c.r, c.g, c.b, 255);
//SDL_RenderDrawPoint(mlx->renderer, x, y);
}
y++;
}
SDL_SetRenderDrawColor(mlx->renderer, 0, 0, 0, 255);
//SDL_SetRenderDrawColor(mlx->renderer, 0, 0, 0, 255);
}

void draw_column(int x, t_mlx *mlx, t_ray *ray)
Expand Down Expand Up @@ -111,8 +130,8 @@ void cast(int col, t_mlx *mlx)
(ray.my + 1.0f - p->y) * ray.deltay;
step_ray(&ray, p, mlx);
ray.wall -= floor(ray.wall);
if (ray.texture)
ray.tex_pos.x = (int)(ray.wall * ray.texture->width);
//if (ray.texture)
// ray.tex_pos.x = (int)(ray.wall * ray.texture->width);
draw_column(col, mlx, &ray);
}

Expand Down
48 changes: 34 additions & 14 deletions wolf3d-nintendoswitch/source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ int main(int argc, char *argv[])
SDL_Event event;
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Surface *surface;
SDL_Texture *texture;
t_mlx stuff;

int done = 0, w = 1280, h = 720;
Expand Down Expand Up @@ -68,7 +70,21 @@ int main(int argc, char *argv[])
SDL_Quit();
return -1;
}
surface = SDL_CreateRGBSurface(0, w, h, 32, 0, 0, 0, 0);
if (!surface)
{
SDL_Log("SDL_CreateRGBSurface: %s\n", SDL_GetError());
SDL_Quit();
return (0);
}

texture = SDL_CreateTextureFromSurface(renderer, surface);
if (!texture)
{
SDL_Log("SDL_CreateTextureFromSurface: %s\n", SDL_GetError());
SDL_Quit();
return (0);
}
// open CONTROLLER_PLAYER_1 and CONTROLLER_PLAYER_2
// when railed, both joycons are mapped to joystick #0,
// else joycons are individually mapped to joystick #0, joystick #1, ...
Expand All @@ -83,15 +99,20 @@ int main(int argc, char *argv[])
int chosen_map = 0;
stuff.h = h;
stuff.w = w;
stuff.renderer = renderer;
stuff.map.matrix = NULL;
stuff.player.x = 0;
stuff.player.y = 0;
stuff.player.cam.x = 0.378560f;
stuff.player.cam.y = -0.540640f;
stuff.player.dir.x = 0.819152f;
stuff.player.dir.y = 0.573576f;

stuff.player.movespeed = 0.1f;
if (!(get_map(&stuff, chosen_map)))
done = 1;
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
while (!done) {
while (SDL_PollEvent(&event)) {
switch (event.type) {
Expand All @@ -101,32 +122,32 @@ int main(int argc, char *argv[])
event.jaxis.axis, event.jaxis.value);
if( event.jaxis.which == 0 )
{
const int JOYSTICK_DEAD_ZONE = 8000;
const int JOYSTICK_DEAD_ZONE = 14000;
//X axis motion
if( event.jaxis.axis == 0 )
{
//Left of dead zone
if( event.jaxis.value < -JOYSTICK_DEAD_ZONE )
{
rotate_player(5.0f / 180.0f * M_PI, &stuff);
rotate_player(3.0f / 180.0f * M_PI, &stuff);
}
//Right of dead zone
else if( event.jaxis.value > JOYSTICK_DEAD_ZONE )
{
rotate_player(-5.0f / 180.0f * M_PI, &stuff);
rotate_player(-3.0f / 180.0f * M_PI, &stuff);
}
}
else if( event.jaxis.axis == 1 )
{
//Below of dead zone
if( event.jaxis.value < -JOYSTICK_DEAD_ZONE )
{
move_player(&stuff, -stuff.player.movespeed);
move_player(&stuff, stuff.player.movespeed);
}
//Above of dead zone
else if( event.jaxis.value > JOYSTICK_DEAD_ZONE )
{
move_player(&stuff, stuff.player.movespeed);
move_player(&stuff, -stuff.player.movespeed);
}
}
}
Expand Down Expand Up @@ -174,16 +195,15 @@ int main(int argc, char *argv[])
break;
}
}

SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);

// fill window bounds
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_GetWindowSize(window, &w, &h);
SDL_Rect f = {0, 0, w, h};
SDL_RenderFillRect(renderer, &f);
stuff.pixels = (uint32_t *)surface->pixels;
memset(stuff.pixels, 0, w * h * sizeof(uint32_t));

wolf_draw(&stuff);

SDL_UpdateTexture(texture, nullptr, surface->pixels, surface->pitch);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, nullptr, nullptr);
SDL_RenderPresent(renderer);
}
if (stuff.map.matrix)
Expand Down
5 changes: 4 additions & 1 deletion wolf3d-nintendoswitch/source/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ static int splint(t_mlx *mlx, int row, char **strs)

fail = false;
i = -1;
if (row != 0 && row != mlx->map.height - 1 &&
(*strs[0] != '1' || *(strs[mlx->map.width - 1]) != '1'))
fail = true;
while (++i < mlx->map.width)
{
//map->matrix[map->width * row + i] = atoi(strs[i]);
Expand All @@ -103,7 +106,7 @@ static int splint(t_mlx *mlx, int row, char **strs)
+ (mlx->map.width * row + i)) != 1)
fail = true;
if (*((mlx->map.matrix) + (mlx->map.width * row + i)) == 0 &&
!mlx->player.x && !mlx->player.y)
mlx->player.x == 0 && mlx->player.y == 0)
{
mlx->player.x = i + 0.5f;
mlx->player.y = row + 0.5f;
Expand Down
1 change: 0 additions & 1 deletion wolf3d-nintendoswitch/source/wolf.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ struct s_mlx
int h;
t_image *tex[TEXTURE_NB];
uint32_t *pixels;
SDL_Renderer *renderer;
t_player player;
t_map map;
};
Expand Down
1 change: 0 additions & 1 deletion wolf3d_maps/boundary.map.bad

This file was deleted.

4 changes: 0 additions & 4 deletions wolf3d_maps/full.map.bad

This file was deleted.

3 changes: 0 additions & 3 deletions wolf3d_maps/texerr.map.bad

This file was deleted.

0 comments on commit eb3a274

Please sign in to comment.