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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
/*AS.h*/
#ifndef AS_H_INCLUDED
#define AS_H_INCLUDED
#include "SDL.h"
#include "SDL_image.h"
#include "SDL_ttf.h"
#include <string>
namespace AS
{
/*types*/
extern int AS_Error;
/* Function */
int AS_Init();
void AS_Quit();
void AS_getError();
SDL_Rect* getRect(int x,int y, int w, int h);
/* Classes */
class Texture{
private:
SDL_Texture *tex = nullptr;
public:
int Load(std::string name,SDL_Renderer* render);
int Load(SDL_Surface* sur,SDL_Renderer* render);
SDL_Texture* getTex();
void Destroy();
};
class Text{
TTF_Font *myfont;
SDL_Color mycolor = {0,0,0};
std::string mesText;
SDL_Surface *mes;
public:
int setColor(Uint8 r,Uint8 g,Uint8 b);
int setFont(std::string loc,int Fsize);
void setText(std::string mtext);
SDL_Surface* getText();
void Destroy();
};
}
#endif // AS_H_INCLUDED
/*AS.cpp*/
#include "AS.h"
int AS::AS_Error;
int AS::AS_Init()
{
if(SDL_Init(SDL_INIT_EVERYTHING) != 0)
{
AS::AS_Error = 1;
return -1;
}
TTF_Init();
return 0;
}
void AS::AS_Quit()
{
TTF_Quit();
SDL_Quit();
}
void AS::Texture::Destroy()
{
SDL_DestroyTexture(tex);
}
int AS::Texture::Load(std::string name,SDL_Renderer* render)
{
SDL_Surface *temp = nullptr;
temp = IMG_Load(name.c_str());
if(temp == nullptr)
{
AS::AS_Error = 2; // cant load
return -1;
}
tex = SDL_CreateTextureFromSurface(render,temp);
SDL_FreeSurface(temp);
return 0;
}
int AS::Texture::Load(SDL_Surface* sur,SDL_Renderer* render)
{
if(sur == nullptr)
{
AS::AS_Error = 3;
return -1;
}
tex = SDL_CreateTextureFromSurface(render,sur);
return 0;
}
SDL_Texture* AS::Texture::getTex()
{
return tex;
}
int AS::Text::setColor(Uint8 r, Uint8 g, Uint8 b)
{
mycolor = {r,g,b};
return 0;
}
int AS::Text::setFont(std::string loc,int Fsize)
{
myfont = TTF_OpenFont(loc.c_str(),Fsize);
if(myfont == nullptr)
{
AS::AS_Error = 4;
return -1;
}
return 0;
}
void AS::Text::setText(std::string mtext)
{
mesText = mtext;
}
SDL_Surface* AS::Text::getText()
{
mes = TTF_RenderText_Solid(myfont,mesText.c_str(),mycolor);
if(mes == nullptr)
{
AS::AS_Error = 5;
}
return mes;
}
void AS::Text::Destroy()
{
TTF_CloseFont(myfont);
}
SDL_Rect* AS::getRect(int x, int y ,int w, int h)
{
SDL_Rect rec;
rec.h = h;
rec.y = y;
rec.w = w;
rec.x = x;
return &rec;
}
/*test.cpp*/
#include "AS.h"
#include <iostream>
using namespace AS;
int main(int argc, char* argv[])
{
AS_Init();
SDL_Window *win;
SDL_Renderer *screen;
Text mess;
SDL_Surface *temp;
Texture backg,textd;
win = SDL_CreateWindow("Text",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,640,480,SDL_WINDOW_SHOWN);
screen = SDL_CreateRenderer(win,-1,SDL_RENDERER_ACCELERATED);
mess.setColor(0,0,0);
mess.setFont("arial.ttf",12);
std::cout << AS::AS_Error << std::endl;
mess.setText("Hello World");
temp = mess.getText();
std::cout << AS::AS_Error << std::endl;
textd.Load(temp,screen);
std::cout << AS::AS_Error << std::endl;
backg.Load("background.png",screen);
SDL_RenderCopy(screen,backg.getTex(),NULL,NULL);
SDL_RenderCopy(screen,textd.getTex(),NULL,getRect(100,100,100,100));
SDL_RenderPresent(screen);
SDL_Delay(2500);
AS_Quit();
return 0;
}
|