28/10/2023 - oregon ha scritto:
Non si manda un link per fare la revisione di tutto un codice. Non funziona così il forum.
Descrivi in dettaglio il problema e posta qui il codice in cui si manifesta e puoi avere qualche dritta o ccconsiglio.
Mi scuso, non avevo letto nulla al riguardo nel regolamento, tuttavia trattandosi di molti file dipendenti tra loro dovrei comunque postare mezzo progetto. A parte ciò, la mia difficoltà stava nel renderizzare le tiles della mappa, l'idea di base è un ciclo innestato che crea la mappa tile per tile in base alle Macros MAP_WIDTH e MAP_HEIGHT.
main
SDL_Texture *loadtexture(char *filename, t_app *app)
{
SDL_Texture *texture;
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Loading %s", filename);
texture = IMG_LoadTexture(app->renderer, filename);
return texture;
}
void setup_character(t_player *player_character, t_app *app)
{
player_character->pos_x = 50;
player_character->pos_y = 50;
player_character->texture = loadtexture("img/sprite.bmp", app);
}
int main()
{
t_player *player_character;
t_menager *menager;
t_app *app;
t_level *level;
SDL_Texture *tile;
// Allocation of custom structures
player_character = (t_player *)malloc(sizeof(t_player));
menager = (t_menager *)malloc(sizeof(t_menager));
app = (t_app *)malloc(sizeof(t_app));
level = (t_level *)malloc(sizeof(t_level));
// Filling my App structure
app->window = initialize_canvas();
app->renderer = initialize_renderer(app->window);
////////////////////////////////////////////////////
// Filling Menager : program handling
menager->game_is_running = FALSE;
menager->game_is_running = initialize_window(app->window, app->renderer);
menager->last_frame_time = 0;
setup_character(player_character, app);
// Initializing the Map
//read_map(av[1]);
tile = load_tiles(app);
initLevel(level, tile, app);
///////////////////////////////////////////////////////
while (menager->game_is_running)
{
set_canvas(app);
// Handle any user input
process_input(menager, app);
handle_movement(player_character, app, menager);
// Update all objects eg. positions ecc.
update(menager);
drawMapTiles(level, tile, app);
// Render changes to the display
render(app, player_character);
present_renderer(app);
}
// Function for cleaning
destroy_window(app->window, app->renderer);
free(player_character);
free(menager);
free(app);
return (0);
}
render.c
// The blit function simply draws the specified texture on screen at the specified x and y coordinates.
void blit(SDL_Texture *texture, t_app *app, int x, int y)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;
if (SDL_QueryTexture(texture, NULL, NULL, &dest.w, &dest.h) == 1)
{
printf("Error in rendering the texture\n");
}
SDL_RenderCopy(app->renderer, texture, NULL, &dest);
}
void render(t_app *app, t_player *player_character)
{
// Renders the character sprite
blit(player_character->texture, app, player_character->pos_x, player_character->pos_y);
}
void present_renderer(t_app *app)
{
// switches front with back buffer
//SDL_RenderPresent(app->renderer);
SDL_RenderPresent(app->renderer);
}
map.c
void drawMapTiles(t_level *level, SDL_Texture *tile, t_app *app)
{
int x;
int y;
int n;
for (y = 0; y < MAP_HEIGHT; y++)
{
for (x = 0; x < MAP_WIDTH; x++)
{
n = level->map.size[x][y];
if (n > 0)
{
//blitAtlasImage(tiles[n], (x * TILE_SIZE) + level->renderOffset.x,
//(y * TILE_SIZE) + level->renderOffset.y, 0, SDL_FLIP_NONE);
blit(tile, app, (x * TILE_SIZE) + level->renderOffset.x,
(y * TILE_SIZE) + level->renderOffset.y);
}
}
}
}
// static void loadTiles(void)
// {
// int i;
// char filename[20];
// i = 0;
// while (i <= 64)
// {
// sprintf(filename, "img/grass.bmp", i);
// tiles[i] = getAtlasImage(filename, 0);
// i++;
// }
// }
void initMap(t_level *level)
{
int x;
int y;
//loadTiles();
x = 0;
y = 0;
while (x < MAP_WIDTH)
{
while (y > MAP_HEIGHT)
{
level->map.size[x][y] = TILE_GROUND;
y++;
}
x++;
}
}
header
#ifndef LIB_H
# define LIB_H
/////////////////////////////////////////////////
// Librerie
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
////////////////////////////////////////////////
// Macros/Constants
#define TRUE 1
#define FALSE 0
#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480
#define FPS 30
// duration of a frame 33.333 ms
#define FRAME_TARGET_TIME (1000 / FPS)
// size of the map
#define MAP_WIDTH 33
#define MAP_HEIGHT 19
#define MAP_RENDER_WIDTH 32
#define MAP_RENDER_HEIGHT 18
#define MAP_RENDER_X ((WINDOW_WIDTH - (MAP_RENDER_WIDTH * MAP_TILE_SIZE)) / 2)
#define MAP_RENDER_Y ((WINDOW_HEIGHT - (MAP_RENDER_HEIGHT * MAP_TILE_SIZE)) / 2)
// defining size of a tile
#define TILE_SIZE 48
// defining tiles references
#define TILE_GROUND 0
#define TILE_WALL 1
////////////////////////////////////////////////
// Structures
typedef struct s_app
{
SDL_Renderer *renderer;
SDL_Window *window;
// Track movement requests
int up;
int down;
int left;
int right;
} t_app;
////////////////////////////////////////////////
typedef struct s_player
{
float pos_x;
float pos_y;
SDL_Texture *texture;
} t_player;
////////////////////////////////////////////////
typedef struct s_menager
{
int last_frame_time;
float delta_time;
int game_is_running;
} t_menager;
////////////////////////////////////////////////
typedef struct s_map
{
int size [MAP_WIDTH][MAP_HEIGHT];
} t_map;
////////////////////////////////////////////////
typedef struct s_level
{
// Contains x & y coordinates
SDL_Point renderOffset;
t_player player;
// t_entity *entity;
// reference to the next entity on the map
t_map map;
} t_level;
////////////////////////////////////////////////
// Functions
void process_input(t_menager *menager, t_app *app);
void render(t_app *app, t_player *player_character);
int initialize_window(SDL_Window *game_canvas, SDL_Renderer *renderer);
void blit(SDL_Texture *texture, t_app *app, int x, int y);
SDL_Window *initialize_canvas();
SDL_Renderer *initialize_renderer(SDL_Window *game_canvas);
SDL_Surface *initialize_background();
void doKeyDown(SDL_KeyboardEvent *event, t_app *app, t_menager *menager);
void doKeyUp(SDL_KeyboardEvent *event, t_app *app);
void handle_movement(t_player *player_character, t_app *app, t_menager *menager);
void initLevel(t_level *level, SDL_Texture *tile, t_app *app);
void initMap(t_level *level);
void drawMapTiles(t_level *level, SDL_Texture *tile, t_app *app);
void present_renderer(t_app *app);
////////////////////////////////////////////////
#endif