122 lines
4.2 KiB
C
122 lines
4.2 KiB
C
/*
|
|
* Ejemplo básico de FnixLib: ventana, formas, teclado, ratón y delta time.
|
|
*
|
|
* Controles:
|
|
* Flechas o WASD mover el cuadrado
|
|
* Ratón un anillo sigue al cursor; mantener el botón izquierdo dibuja
|
|
* Escape salir
|
|
*
|
|
* Opción para pruebas automáticas:
|
|
* ./basic --frames N termina solo tras N fotogramas
|
|
*/
|
|
#include <fnix/fnix.h>
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
int max_frames = 0; /* 0 = sin límite */
|
|
if (argc == 3 && strcmp(argv[1], "--frames") == 0) {
|
|
max_frames = atoi(argv[2]);
|
|
}
|
|
|
|
FnixWindow *window = fnix_window_create("FnixLib - ejemplo básico", 800, 600);
|
|
if (window == NULL) {
|
|
fprintf(stderr, "Error al crear la ventana: %s\n", fnix_last_error());
|
|
return 1;
|
|
}
|
|
|
|
const int radius = 24;
|
|
float ball_x = 400.0f, ball_y = 300.0f;
|
|
float ball_vx = 260.0f, ball_vy = 190.0f;
|
|
float box_x = 100.0f, box_y = 100.0f;
|
|
const float box_size = 48.0f;
|
|
const float box_speed = 300.0f;
|
|
|
|
int frames = 0;
|
|
double fps_timer = 0.0;
|
|
int fps_frames = 0;
|
|
|
|
while (!fnix_window_should_close(window)) {
|
|
fnix_begin_frame(window);
|
|
const float dt = (float)fnix_window_delta_time(window);
|
|
|
|
if (fnix_key_pressed(window, FNIX_KEY_ESCAPE)) {
|
|
fnix_window_request_close(window);
|
|
}
|
|
|
|
/* Cuadrado controlado por el teclado. */
|
|
if (fnix_key_down(window, FNIX_KEY_LEFT) || fnix_key_down(window, FNIX_KEY_A)) {
|
|
box_x -= box_speed * dt;
|
|
}
|
|
if (fnix_key_down(window, FNIX_KEY_RIGHT) || fnix_key_down(window, FNIX_KEY_D)) {
|
|
box_x += box_speed * dt;
|
|
}
|
|
if (fnix_key_down(window, FNIX_KEY_UP) || fnix_key_down(window, FNIX_KEY_W)) {
|
|
box_y -= box_speed * dt;
|
|
}
|
|
if (fnix_key_down(window, FNIX_KEY_DOWN) || fnix_key_down(window, FNIX_KEY_S)) {
|
|
box_y += box_speed * dt;
|
|
}
|
|
if (box_x < 0.0f) box_x = 0.0f;
|
|
if (box_y < 0.0f) box_y = 0.0f;
|
|
if (box_x > 800.0f - box_size) box_x = 800.0f - box_size;
|
|
if (box_y > 600.0f - box_size) box_y = 600.0f - box_size;
|
|
|
|
/* Pelota que rebota. */
|
|
ball_x += ball_vx * dt;
|
|
ball_y += ball_vy * dt;
|
|
if (ball_x < (float)radius) { ball_x = (float)radius; ball_vx = -ball_vx; }
|
|
if (ball_x > 800.0f - (float)radius) { ball_x = 800.0f - (float)radius; ball_vx = -ball_vx; }
|
|
if (ball_y < (float)radius) { ball_y = (float)radius; ball_vy = -ball_vy; }
|
|
if (ball_y > 600.0f - (float)radius) { ball_y = 600.0f - (float)radius; ball_vy = -ball_vy; }
|
|
|
|
/* Dibujo. */
|
|
fnix_clear(FNIX_RGB(24, 24, 32));
|
|
|
|
for (int x = 0; x <= 800; x += 50) {
|
|
fnix_draw_line(x, 0, x, 599, FNIX_RGB(40, 40, 56));
|
|
}
|
|
for (int y = 0; y <= 600; y += 50) {
|
|
fnix_draw_line(0, y, 799, y, FNIX_RGB(40, 40, 56));
|
|
}
|
|
|
|
fnix_draw_rect_outline(0, 0, 800, 600, FNIX_GRAY);
|
|
fnix_draw_circle((int)ball_x, (int)ball_y, radius, FNIX_YELLOW);
|
|
fnix_draw_circle_outline((int)ball_x, (int)ball_y, radius + 6, FNIX_RGBA(255, 255, 0, 110));
|
|
fnix_draw_rect((int)box_x, (int)box_y, (int)box_size, (int)box_size,
|
|
FNIX_RGBA(80, 160, 255, 200));
|
|
fnix_draw_rect_outline((int)box_x, (int)box_y, (int)box_size, (int)box_size, FNIX_WHITE);
|
|
|
|
const int mx = fnix_mouse_x(window);
|
|
const int my = fnix_mouse_y(window);
|
|
if (fnix_mouse_down(window, FNIX_MOUSE_LEFT)) {
|
|
fnix_draw_circle(mx, my, 10, FNIX_RGBA(255, 80, 80, 160));
|
|
}
|
|
fnix_draw_circle_outline(mx, my, 14, FNIX_WHITE);
|
|
fnix_draw_line(mx - 20, my, mx + 20, my, FNIX_WHITE);
|
|
fnix_draw_line(mx, my - 20, mx, my + 20, FNIX_WHITE);
|
|
|
|
fnix_end_frame(window);
|
|
|
|
/* FPS por la salida estándar, una vez por segundo. */
|
|
fps_timer += (double)dt;
|
|
fps_frames++;
|
|
if (fps_timer >= 1.0) {
|
|
printf("FPS: %.1f\n", (double)fps_frames / fps_timer);
|
|
fflush(stdout);
|
|
fps_timer = 0.0;
|
|
fps_frames = 0;
|
|
}
|
|
|
|
if (max_frames > 0 && ++frames >= max_frames) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
fnix_window_destroy(window);
|
|
return 0;
|
|
}
|