1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
using namespace std;
const int SCREENHEIGHT = 600;
const int SCREENWIDTH = 600;
bool ballLeft,ballRight,ballUp,ballDown;
bool racketOneUp,racketOneDown;
SDL_Window *window;
SDL_Renderer *renderer;
TTF_Font *font = NULL;
SDL_Event event;
SDL_Surface *ballSurface;
SDL_Surface *racketSurface;
SDL_Surface *fontSurface = NULL;
SDL_Texture *fontTexture;
SDL_Texture *ball;
SDL_Texture *racket;
SDL_Rect *ballRectDst;
SDL_Rect *ballRectSrc;
SDL_Rect *racketRectDst;
SDL_Rect *racketRectSrc;
SDL_Rect *fontRectDst;
SDL_Color color;
bool init()
{
if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
return false;
}
if(IMG_Init(IMG_INIT_PNG) < 0)
{
return false;
}
if(TTF_Init() < 0){
return false;
}
window = SDL_CreateWindow("PONG",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,SCREENWIDTH,SCREENHEIGHT,SDL_WINDOW_RESIZABLE);
if(!window)
{
cout << "could not create window" << endl;
return false;
}
renderer = SDL_CreateRenderer(window,-1,0);
if(!renderer)
{
cout << "could not create renderer" << endl;
return false;
}
fontRectDst = new SDL_Rect;
SDL_SetRenderDrawColor(renderer,255,255,255,255);
ballSurface = IMG_Load("ball.png");
racketSurface = IMG_Load("racket.png");
font = TTF_OpenFont("PTN77F.ttf",100);
SDL_Color nothercolor = {130, 170, 190};
fontRectDst->h = 80;
fontRectDst->w = 90;
fontRectDst->x = 100;
fontRectDst->y = 110;
fontSurface = TTF_RenderText_Solid(font,"hello hello",nothercolor);
ball = SDL_CreateTextureFromSurface(renderer,ballSurface);
racket = SDL_CreateTextureFromSurface(renderer,racketSurface);
ballRectSrc = new SDL_Rect;
ballRectDst = new SDL_Rect;
racketRectSrc = new SDL_Rect;
racketRectDst = new SDL_Rect;
ballRectDst->h = 80;
ballRectDst->w = 80;
ballRectDst->x = SCREENWIDTH / 2;
ballRectDst->y = SCREENHEIGHT / 2;
racketRectDst->h = 120;
racketRectDst->w = 40;
racketRectDst->x = 5;
racketRectDst->y = 250;
ballLeft = false;
ballRight = true;
ballUp = false;
ballDown = false;
racketOneDown = false;
racketOneUp = false;
return true;
}
| |