CC/sdl_emscripten/main.c

206 lines
5.3 KiB
C
Raw Normal View History

2024-01-27 23:59:02 +03:00
#include <stdio.h>
#include <assert.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
int main(int argc, char **argv) {
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
printf("error initializing SDL: %s\n", SDL_GetError());
return 1;
}
SDL_Window *window = NULL;
window =
SDL_CreateWindow("Hello, SDL 2!", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, 0);
if (window == NULL) {
return 1;
}
SDL_Renderer* renderer = NULL;
renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawColor( renderer, 255, 0, 0, 255 );
SDL_RenderClear( renderer );
SDL_Rect dest;
dest.w= 50;
dest.h= 50;
dest.x = 10;
dest.y = 10;
SDL_SetRenderDrawColor( renderer, 0, 0, 255, 255 );
SDL_RenderFillRect( renderer, &dest );
// SDL_Surface *image = IMG_Load("girl_faces.png");
// if (!image) {
// printf("IMG_Load: %s\n", IMG_GetError());
// return 0;
// }
// SDL_Texture *tex = SDL_CreateTextureFromSurface(rend, image);
// SDL_FreeSurface(image);
// SDL_QueryTexture(tex, NULL, NULL, &dest.w, &dest.h);
// SDL_RenderClear(rend);
// SDL_RenderCopy(rend, tex, NULL, &dest);
SDL_RenderPresent(renderer);
SDL_Delay(2000);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
// printf("hello, world!\n");
// puts("0");
// if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
// printf("error initializing SDL: %s\n", SDL_GetError());
// return 1;
// }
// SDL_Surface *screen_surface = NULL;
// SDL_Window *window = NULL;
// puts("1");
// window =
// SDL_CreateWindow("Hello, SDL 2!", SDL_WINDOWPOS_CENTERED,
// SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT,
// 0);
// if (window == NULL) {
// return 1;
// }
// Uint32 render_flags = SDL_RENDERER_ACCELERATED;
// // creates a renderer to render our images
// SDL_Renderer* rend = SDL_CreateRenderer(window, -1, render_flags);
// screen_surface = SDL_GetWindowSurface(window);
// // creates a surface to load an image into the main memory
// SDL_Surface *surface;
// // please provide a path for your image
// surface = IMG_Load("girl_faces.png");
// // loads image to our graphics hardware memory.
// SDL_Texture *tex = SDL_CreateTextureFromSurface(rend, surface);
// // clears main-memory
// SDL_FreeSurface(surface);
// // let us control our image position
// // so that we can move it with our keyboard.
// SDL_Rect dest;
// // connects our texture with dest to control position
// SDL_QueryTexture(tex, NULL, NULL, &dest.w, &dest.h);
// // adjust height and width of our image box.
// dest.w /= 6;
// dest.h /= 6;
// // sets initial x-position of object
// dest.x = (1000 - dest.w) / 2;
// // sets initial y-position of object
// dest.y = (1000 - dest.h) / 2;
// puts("13");
// // controls animation loop
// int close = 0;
// // speed of box
// int speed = 300;
// // animation loop
// while (!close) {
// SDL_Event event;
// // Events management
// while (SDL_PollEvent(&event)) {
// switch (event.type) {
// case SDL_QUIT:
// // handling of close button
// close = 1;
// break;
// case SDL_KEYDOWN:
// // keyboard API for key pressed
// switch (event.key.keysym.scancode) {
// case SDL_SCANCODE_ESCAPE:
// close = 1;
// break;
// case SDL_SCANCODE_W:
// case SDL_SCANCODE_UP:
// dest.y -= speed / 30;
// break;
// case SDL_SCANCODE_A:
// case SDL_SCANCODE_LEFT:
// dest.x -= speed / 30;
// break;
// case SDL_SCANCODE_S:
// case SDL_SCANCODE_DOWN:
// dest.y += speed / 30;
// break;
// case SDL_SCANCODE_D:
// case SDL_SCANCODE_RIGHT:
// dest.x += speed / 30;
// break;
// default:
// break;
// }
// }
// }
// // right boundary
// if (dest.x + dest.w > 1000)
// dest.x = 1000 - dest.w;
// // left boundary
// if (dest.x < 0)
// dest.x = 0;
// // bottom boundary
// if (dest.y + dest.h > 1000)
// dest.y = 1000 - dest.h;
// // upper boundary
// if (dest.y < 0)
// dest.y = 0;
// // clears the screen
// SDL_RenderClear(rend);
// SDL_RenderCopy(rend, tex, NULL, &dest);
// ///
// /// Section 4: SDL ttf and rendering text
// ///
// // triggers the double buffers
// // for multiple rendering
// SDL_RenderPresent(rend);
// // calculates to 60 fps
// SDL_Delay(1000 / 60);
// }
// SDL_Delay(2000);
// SDL_DestroyWindow(window);
// SDL_Quit();
}