151 lines
2.8 KiB
C
151 lines
2.8 KiB
C
#include "SDL2/SDL.h"
|
|
#include <SDL2/SDL_events.h>
|
|
#include <SDL2/SDL_hints.h>
|
|
#include <SDL2/SDL_rect.h>
|
|
#include <SDL2/SDL_render.h>
|
|
#include <SDL2/SDL_timer.h>
|
|
#include <SDL2/SDL_video.h>
|
|
#include <SDL2/SDL_image.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include "../include/vscreen.h"
|
|
|
|
const int SCREEN_WIDTH = 1280;
|
|
const int SCREEN_HEIGHT = 720;
|
|
|
|
typedef struct {
|
|
SDL_Window *window;
|
|
SDL_Renderer *renderer;
|
|
int up;
|
|
int down;
|
|
int left;
|
|
int right;
|
|
} App;
|
|
|
|
typedef struct {
|
|
int x;
|
|
int y;
|
|
SDL_Texture *texture;
|
|
} Entity;
|
|
|
|
App app;
|
|
|
|
Entity player;
|
|
|
|
void handleInput(void);
|
|
void cleanup(void);
|
|
void prepareScene(void);
|
|
void presentScene(void);
|
|
SDL_Texture *loadTexture(char *);
|
|
void blit(SDL_Texture *, int, int);
|
|
int getIndex(int, int, int);
|
|
|
|
int main(int argc, char* args[]) {
|
|
memset(&app, 0, sizeof(app));
|
|
memset(&player, 0, sizeof(player));
|
|
|
|
player.x = 100;
|
|
player.y = 100;
|
|
player.texture = loadTexture("text.jpg");
|
|
|
|
IMG_Init(IMG_INIT_JPG);
|
|
|
|
atexit(cleanup);
|
|
|
|
if (SDL_Init(SDL_INIT_VIDEO) < 0){
|
|
printf("SDL failed: %s\n", SDL_GetError());
|
|
return -1;
|
|
}
|
|
|
|
app.window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
|
|
|
|
if (app.window == NULL){
|
|
printf("Window creation failed: %s\n", SDL_GetError());
|
|
return -1;
|
|
}
|
|
|
|
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
|
|
|
|
app.renderer = SDL_CreateRenderer(app.window, -1, SDL_RENDERER_ACCELERATED);
|
|
|
|
if (app.renderer == NULL) {
|
|
printf("Renderer failed: %s\n", SDL_GetError());
|
|
return -1;
|
|
}
|
|
|
|
while(1) {
|
|
prepareScene();
|
|
|
|
handleInput();
|
|
|
|
//blit(player.texture, player.x, player.y);
|
|
|
|
presentScene();
|
|
|
|
SDL_Delay(16);
|
|
}
|
|
}
|
|
|
|
void blit(SDL_Texture *texture, int x, int y)
|
|
{
|
|
SDL_Rect dest;
|
|
|
|
dest.x = x;
|
|
dest.y = y;
|
|
SDL_QueryTexture(texture, NULL, NULL, &dest.w, &dest.h);
|
|
|
|
SDL_RenderCopy(app.renderer, texture, NULL, &dest);
|
|
}
|
|
|
|
SDL_Texture* loadTexture(char *filePath) {
|
|
SDL_Texture *texture;
|
|
|
|
texture = IMG_LoadTexture(app.renderer, filePath);
|
|
|
|
return texture;
|
|
}
|
|
|
|
void cleanup(void) {
|
|
SDL_DestroyWindow(app.window);
|
|
SDL_DestroyRenderer(app.renderer);
|
|
SDL_Quit();
|
|
}
|
|
|
|
void handleInput(void) {
|
|
SDL_Event event;
|
|
|
|
while(SDL_PollEvent(&event)) {
|
|
switch (event.type) {
|
|
case SDL_QUIT:
|
|
exit(0);
|
|
break;
|
|
case SDL_KEYDOWN:
|
|
player.x += 1;
|
|
player.y += 1;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
void prepareScene(void)
|
|
{
|
|
SDL_SetRenderDrawColor(app.renderer, 96, 128, 255, 255);
|
|
SDL_RenderClear(app.renderer);
|
|
|
|
SDL_Rect rect = {0, 0, VSCREEN_WIDTH, VSCREEN_HEIGHT};
|
|
|
|
SDL_RenderSetViewport(app.renderer, &rect);
|
|
|
|
renderClientScreen(app.renderer);
|
|
}
|
|
|
|
void presentScene(void)
|
|
{
|
|
SDL_RenderPresent(app.renderer);
|
|
}
|
|
|
|
int getIndex(int x, int y, int columns) {
|
|
return x + y * columns;
|
|
} |