mirror of
https://github.com/sam-astro/Z-Sharp.git
synced 2025-12-13 09:02:10 +00:00
aaaaaaaaaaaaaAAAAAAAAAAAAAAAAAAAAAGHGHGHGGHGHHGHGHGGHGHGHGHGHGHGHGHGHHG
This commit is contained in:
parent
59d0bd5c56
commit
ab015b0cef
@ -558,29 +558,29 @@ int parseSlang(string script)
|
|||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
//
|
|
||||||
//int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
//{
|
{
|
||||||
// // Get builtin script contents
|
// Get builtin script contents
|
||||||
// ifstream builtin("../Slang/builtin.slg");
|
ifstream builtin("../Slang/builtin.slg");
|
||||||
// stringstream builtinString;
|
stringstream builtinString;
|
||||||
// builtinString << builtin.rdbuf();
|
builtinString << builtin.rdbuf();
|
||||||
//
|
|
||||||
// // Gathers builtin functions and variables
|
// Gathers builtin functions and variables
|
||||||
// GetBuiltins(builtinString.str());
|
GetBuiltins(builtinString.str());
|
||||||
// functionValues = builtinFunctionValues;
|
functionValues = builtinFunctionValues;
|
||||||
// globalVariableValues = builtinVarVals;
|
globalVariableValues = builtinVarVals;
|
||||||
//
|
|
||||||
// // Get default script contents
|
// Get default script contents
|
||||||
// ifstream script("../Slang/script.slg");
|
ifstream script("../Slang/script.slg");
|
||||||
// stringstream scriptString;
|
stringstream scriptString;
|
||||||
// scriptString << script.rdbuf();
|
scriptString << script.rdbuf();
|
||||||
//
|
|
||||||
// while (true) {
|
while (true) {
|
||||||
// system("pause");
|
system("pause");
|
||||||
// break;
|
break;
|
||||||
// }
|
}
|
||||||
// parseSlang(scriptString.str());
|
parseSlang(scriptString.str());
|
||||||
//
|
|
||||||
// return 0;
|
return 0;
|
||||||
//}
|
}
|
||||||
|
|||||||
@ -150,7 +150,6 @@
|
|||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="eval.cpp" />
|
<ClCompile Include="eval.cpp" />
|
||||||
<ClCompile Include="graphics.cpp" />
|
|
||||||
<ClCompile Include="Main.cpp" />
|
<ClCompile Include="Main.cpp" />
|
||||||
<ClCompile Include="strops.cpp" />
|
<ClCompile Include="strops.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@ -24,9 +24,6 @@
|
|||||||
<ClCompile Include="strops.cpp">
|
<ClCompile Include="strops.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<ClCompile Include="graphics.cpp">
|
|
||||||
<Filter>Source Files</Filter>
|
|
||||||
</ClCompile>
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="olcPixelGameEngine.h">
|
<ClInclude Include="olcPixelGameEngine.h">
|
||||||
|
|||||||
@ -17,7 +17,7 @@
|
|||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace boost;
|
using namespace boost;
|
||||||
|
|
||||||
vector<string> types = { "int", "float", "string", "bool", "void", "null", "Circle"};
|
vector<string> types = { "int", "float", "string", "bool", "void", "null", "Sprite"};
|
||||||
|
|
||||||
unordered_map<string, vector<vector<string>>> builtinFunctionValues;
|
unordered_map<string, vector<vector<string>>> builtinFunctionValues;
|
||||||
unordered_map<string, boost::any> builtinVarVals;
|
unordered_map<string, boost::any> builtinVarVals;
|
||||||
@ -123,18 +123,15 @@ boost::any CPPFunction(const string& name, const vector<boost::any>& args)
|
|||||||
return AnyAsInt(args[0]);
|
return AnyAsInt(args[0]);
|
||||||
else if (name == "CPP.Graphics.Init")
|
else if (name == "CPP.Graphics.Init")
|
||||||
{
|
{
|
||||||
/*cout << "\x1B[32mInit graphics\033[0m\t\t" << endl;
|
cout << "\x1B[32mInit graphics\033[0m\t\t" << endl;
|
||||||
mainWindow.Start(AnyAsInt(args[0]), AnyAsInt(args[1]));*/
|
initGraphics(AnyAsString(args[0]), AnyAsInt(args[1]), AnyAsInt(args[2]));
|
||||||
}
|
}
|
||||||
else if (name == "CPP.Graphics.Circle")
|
else if (name == "CPP.Graphics.Sprite")
|
||||||
{
|
{
|
||||||
/*Circle c;
|
Sprite s(AnyAsString(args[0]), any_cast<Vec2>(args[1]), any_cast<Vec2>(args[2]), AnyAsFloat(args[3]));
|
||||||
c.x = AnyAsInt(args[0]);
|
boost::any a = s;
|
||||||
c.y = AnyAsInt(args[1]);
|
Sprite d = any_cast<Sprite>(a);
|
||||||
c.r = AnyAsInt(args[2]);
|
return d;
|
||||||
boost::any a = c;
|
|
||||||
Circle d = any_cast<Circle>(a);
|
|
||||||
return d;*/
|
|
||||||
}
|
}
|
||||||
else if (name == "CPP.System.Print")
|
else if (name == "CPP.System.Print")
|
||||||
cout << AnyAsString(args[0]);
|
cout << AnyAsString(args[0]);
|
||||||
|
|||||||
@ -78,9 +78,16 @@ func Printl(in)
|
|||||||
CPP.System.PrintLine(in)
|
CPP.System.PrintLine(in)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Creates new circle class
|
// Creates new sprite class
|
||||||
func NewCircle(x, y, r)
|
func NSprite(path, x, y, r)
|
||||||
{
|
{
|
||||||
Circle c = CPP.Graphics.Circle(x, y, r)
|
Sprite s = CPP.Graphics.Sprite(path, x, y, r)
|
||||||
return c
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
// Creates new Vector2 class
|
||||||
|
func Vec2(x, y)
|
||||||
|
{
|
||||||
|
Vec2 v = CPP.Graphics.Vec2(x, y)
|
||||||
|
return v
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,405 +0,0 @@
|
|||||||
#include <chrono>
|
|
||||||
#include <SDL.h>
|
|
||||||
#include <SDL_ttf.h>
|
|
||||||
#include <SDL_image.h>
|
|
||||||
#include <string>
|
|
||||||
//#include "graphics.h"
|
|
||||||
|
|
||||||
const int WINDOW_WIDTH = 1280;
|
|
||||||
const int WINDOW_HEIGHT = 720;
|
|
||||||
|
|
||||||
enum Buttons
|
|
||||||
{
|
|
||||||
PaddleOneUp = 0,
|
|
||||||
PaddleOneDown,
|
|
||||||
PaddleTwoUp,
|
|
||||||
PaddleTwoDown,
|
|
||||||
};
|
|
||||||
|
|
||||||
const float PADDLE_SPEED = 1.0f;
|
|
||||||
|
|
||||||
|
|
||||||
//The window we'll be rendering to
|
|
||||||
SDL_Window* gWindow = NULL;
|
|
||||||
|
|
||||||
//The renderer we'll be rendering to
|
|
||||||
SDL_Renderer* gRenderer = NULL;
|
|
||||||
|
|
||||||
//The surface contained by the window
|
|
||||||
SDL_Surface* gScreenSurface = NULL;
|
|
||||||
|
|
||||||
|
|
||||||
SDL_Surface* loadSurface(std::string path)
|
|
||||||
{
|
|
||||||
//The final optimized image
|
|
||||||
SDL_Surface* optimizedSurface = NULL;
|
|
||||||
|
|
||||||
//Load image at specified path
|
|
||||||
SDL_Surface* loadedSurface = IMG_Load(path.c_str());
|
|
||||||
if (loadedSurface == NULL)
|
|
||||||
{
|
|
||||||
printf("Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
//Convert surface to screen format
|
|
||||||
optimizedSurface = SDL_ConvertSurface(loadedSurface, gScreenSurface->format, 0);
|
|
||||||
if (optimizedSurface == NULL)
|
|
||||||
{
|
|
||||||
printf("Unable to optimize image %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
|
|
||||||
}
|
|
||||||
|
|
||||||
//Get rid of old loaded surface
|
|
||||||
SDL_FreeSurface(loadedSurface);
|
|
||||||
}
|
|
||||||
|
|
||||||
return optimizedSurface;
|
|
||||||
}
|
|
||||||
|
|
||||||
class Vec2
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
Vec2()
|
|
||||||
: x(0.0f), y(0.0f)
|
|
||||||
{}
|
|
||||||
|
|
||||||
Vec2(float x, float y)
|
|
||||||
: x(x), y(y)
|
|
||||||
{}
|
|
||||||
|
|
||||||
Vec2 operator+(Vec2 const& rhs)
|
|
||||||
{
|
|
||||||
return Vec2(x + rhs.x, y + rhs.y);
|
|
||||||
}
|
|
||||||
|
|
||||||
Vec2& operator+=(Vec2 const& rhs)
|
|
||||||
{
|
|
||||||
x += rhs.x;
|
|
||||||
y += rhs.y;
|
|
||||||
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
Vec2 operator*(float rhs)
|
|
||||||
{
|
|
||||||
return Vec2(x * rhs, y * rhs);
|
|
||||||
}
|
|
||||||
|
|
||||||
float x, y;
|
|
||||||
};
|
|
||||||
|
|
||||||
const int BALL_WIDTH = 15;
|
|
||||||
const int BALL_HEIGHT = 15;
|
|
||||||
|
|
||||||
class Ball
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
Ball(Vec2 position)
|
|
||||||
: position(position)
|
|
||||||
{
|
|
||||||
rect.x = static_cast<int>(position.x);
|
|
||||||
rect.y = static_cast<int>(position.y);
|
|
||||||
rect.w = BALL_WIDTH;
|
|
||||||
rect.h = BALL_HEIGHT;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Draw(SDL_Renderer* renderer)
|
|
||||||
{
|
|
||||||
rect.x = static_cast<int>(position.x);
|
|
||||||
rect.y = static_cast<int>(position.y);
|
|
||||||
|
|
||||||
SDL_RenderFillRect(renderer, &rect);
|
|
||||||
}
|
|
||||||
|
|
||||||
Vec2 position;
|
|
||||||
SDL_Rect rect{};
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
const int PADDLE_WIDTH = 15;
|
|
||||||
const int PADDLE_HEIGHT = 80;
|
|
||||||
class Paddle
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
Paddle(Vec2 position, Vec2 velocity)
|
|
||||||
: position(position), velocity(velocity)
|
|
||||||
{
|
|
||||||
rect.x = static_cast<int>(position.x);
|
|
||||||
rect.y = static_cast<int>(position.y);
|
|
||||||
rect.w = PADDLE_WIDTH;
|
|
||||||
rect.h = PADDLE_HEIGHT;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Update(float dt)
|
|
||||||
{
|
|
||||||
position += velocity * dt;
|
|
||||||
|
|
||||||
if (position.y < 0)
|
|
||||||
{
|
|
||||||
// Restrict to top of the screen
|
|
||||||
position.y = 0;
|
|
||||||
}
|
|
||||||
else if (position.y > (WINDOW_HEIGHT - PADDLE_HEIGHT))
|
|
||||||
{
|
|
||||||
// Restrict to bottom of the screen
|
|
||||||
position.y = WINDOW_HEIGHT - PADDLE_HEIGHT;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Draw(SDL_Renderer* renderer)
|
|
||||||
{
|
|
||||||
rect.y = static_cast<int>(position.y);
|
|
||||||
|
|
||||||
SDL_RenderFillRect(renderer, &rect);
|
|
||||||
}
|
|
||||||
|
|
||||||
Vec2 position;
|
|
||||||
Vec2 velocity;
|
|
||||||
SDL_Rect rect{};
|
|
||||||
};
|
|
||||||
|
|
||||||
class Sprite
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
Sprite(Vec2 position, std::string path)
|
|
||||||
: position(position)
|
|
||||||
{
|
|
||||||
rect.x = static_cast<int>(position.x);
|
|
||||||
rect.y = static_cast<int>(position.y);
|
|
||||||
rect.w = PADDLE_WIDTH;
|
|
||||||
rect.h = PADDLE_HEIGHT;
|
|
||||||
|
|
||||||
SDL_Surface* surface = loadSurface(path);
|
|
||||||
texture = SDL_CreateTextureFromSurface(gRenderer, surface);
|
|
||||||
SDL_FreeSurface(surface);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Draw()
|
|
||||||
{
|
|
||||||
rect.y = static_cast<int>(position.y);
|
|
||||||
|
|
||||||
//SDL_RenderFillRect(renderer, &rect);
|
|
||||||
SDL_RenderCopy(gRenderer, texture, NULL, &rect);
|
|
||||||
}
|
|
||||||
|
|
||||||
Vec2 position;
|
|
||||||
std::string path;
|
|
||||||
SDL_Rect rect{};
|
|
||||||
SDL_Texture* texture;
|
|
||||||
};
|
|
||||||
|
|
||||||
class PlayerScore
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
PlayerScore(Vec2 position, SDL_Renderer* renderer, TTF_Font* font, SDL_Color scoreColor)
|
|
||||||
: renderer(renderer), font(font)
|
|
||||||
{
|
|
||||||
surface = TTF_RenderText_Solid(font, "0", scoreColor);
|
|
||||||
texture = SDL_CreateTextureFromSurface(renderer, surface);
|
|
||||||
|
|
||||||
int width, height;
|
|
||||||
SDL_QueryTexture(texture, nullptr, nullptr, &width, &height);
|
|
||||||
|
|
||||||
rect.x = static_cast<int>(position.x);
|
|
||||||
rect.y = static_cast<int>(position.y);
|
|
||||||
rect.w = width;
|
|
||||||
rect.h = height;
|
|
||||||
}
|
|
||||||
|
|
||||||
~PlayerScore()
|
|
||||||
{
|
|
||||||
SDL_FreeSurface(surface);
|
|
||||||
SDL_DestroyTexture(texture);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Draw()
|
|
||||||
{
|
|
||||||
SDL_RenderCopy(renderer, texture, nullptr, &rect);
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_Renderer* renderer;
|
|
||||||
TTF_Font* font;
|
|
||||||
SDL_Surface* surface{};
|
|
||||||
SDL_Texture* texture{};
|
|
||||||
SDL_Rect rect{};
|
|
||||||
};
|
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
|
||||||
{
|
|
||||||
// Initialize SDL components
|
|
||||||
SDL_Init(SDL_INIT_VIDEO);
|
|
||||||
TTF_Init();
|
|
||||||
|
|
||||||
gWindow = SDL_CreateWindow("Pong", 20, 20, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);
|
|
||||||
gRenderer = SDL_CreateRenderer(gWindow, -1, 0);
|
|
||||||
|
|
||||||
//Get window surface
|
|
||||||
gScreenSurface = SDL_GetWindowSurface(gWindow);
|
|
||||||
|
|
||||||
// Initialize the font
|
|
||||||
TTF_Font* scoreFont = TTF_OpenFont("arial.ttf", 40);
|
|
||||||
|
|
||||||
SDL_Color scoreColor = { 0xFF, 0xFF, 0xFF, 0xFF };
|
|
||||||
|
|
||||||
// Create the player score text fields
|
|
||||||
PlayerScore playerOneScoreText(Vec2(WINDOW_WIDTH / 4, 20), gRenderer, scoreFont, scoreColor);
|
|
||||||
|
|
||||||
PlayerScore playerTwoScoreText(Vec2(3 * WINDOW_WIDTH / 4, 20), gRenderer, scoreFont, scoreColor);
|
|
||||||
|
|
||||||
// Create the ball
|
|
||||||
Ball ball(
|
|
||||||
Vec2((WINDOW_WIDTH / 2.0f) - (BALL_WIDTH / 2.0f),
|
|
||||||
(WINDOW_HEIGHT / 2.0f) - (BALL_WIDTH / 2.0f)));
|
|
||||||
|
|
||||||
// Create the paddles
|
|
||||||
Paddle paddleOne(
|
|
||||||
Vec2(50.0f, WINDOW_HEIGHT / 2.0f),
|
|
||||||
Vec2(0.0f, 0.0f));
|
|
||||||
|
|
||||||
Paddle paddleTwo(
|
|
||||||
Vec2(WINDOW_WIDTH - 50.0f, WINDOW_HEIGHT / 2.0f),
|
|
||||||
Vec2(0.0f, 0.0f));
|
|
||||||
|
|
||||||
Sprite randomAssSprite(
|
|
||||||
Vec2((WINDOW_WIDTH / 2.0f) - (100 / 2.0f),
|
|
||||||
(WINDOW_HEIGHT / 2.0f) - (100 / 2.0f)), "./circle.png");
|
|
||||||
|
|
||||||
|
|
||||||
// Game logic
|
|
||||||
{
|
|
||||||
bool running = true;
|
|
||||||
bool buttons[4] = {};
|
|
||||||
float dt = 0.0f;
|
|
||||||
|
|
||||||
// Continue looping and processing events until user exits
|
|
||||||
while (running)
|
|
||||||
{
|
|
||||||
auto startTime = std::chrono::high_resolution_clock::now();
|
|
||||||
|
|
||||||
SDL_Event event;
|
|
||||||
while (SDL_PollEvent(&event))
|
|
||||||
{
|
|
||||||
if (event.type == SDL_QUIT)
|
|
||||||
{
|
|
||||||
running = false;
|
|
||||||
}
|
|
||||||
else if (event.type == SDL_KEYDOWN)
|
|
||||||
{
|
|
||||||
if (event.key.keysym.sym == SDLK_ESCAPE)
|
|
||||||
{
|
|
||||||
running = false;
|
|
||||||
}
|
|
||||||
else if (event.key.keysym.sym == SDLK_w)
|
|
||||||
{
|
|
||||||
buttons[Buttons::PaddleOneUp] = true;
|
|
||||||
}
|
|
||||||
else if (event.key.keysym.sym == SDLK_s)
|
|
||||||
{
|
|
||||||
buttons[Buttons::PaddleOneDown] = true;
|
|
||||||
}
|
|
||||||
else if (event.key.keysym.sym == SDLK_UP)
|
|
||||||
{
|
|
||||||
buttons[Buttons::PaddleTwoUp] = true;
|
|
||||||
}
|
|
||||||
else if (event.key.keysym.sym == SDLK_DOWN)
|
|
||||||
{
|
|
||||||
buttons[Buttons::PaddleTwoDown] = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (event.type == SDL_KEYUP)
|
|
||||||
{
|
|
||||||
if (event.key.keysym.sym == SDLK_w)
|
|
||||||
{
|
|
||||||
buttons[Buttons::PaddleOneUp] = false;
|
|
||||||
}
|
|
||||||
else if (event.key.keysym.sym == SDLK_s)
|
|
||||||
{
|
|
||||||
buttons[Buttons::PaddleOneDown] = false;
|
|
||||||
}
|
|
||||||
else if (event.key.keysym.sym == SDLK_UP)
|
|
||||||
{
|
|
||||||
buttons[Buttons::PaddleTwoUp] = false;
|
|
||||||
}
|
|
||||||
else if (event.key.keysym.sym == SDLK_DOWN)
|
|
||||||
{
|
|
||||||
buttons[Buttons::PaddleTwoDown] = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (buttons[Buttons::PaddleOneUp])
|
|
||||||
{
|
|
||||||
paddleOne.velocity.y = -PADDLE_SPEED;
|
|
||||||
}
|
|
||||||
else if (buttons[Buttons::PaddleOneDown])
|
|
||||||
{
|
|
||||||
paddleOne.velocity.y = PADDLE_SPEED;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
paddleOne.velocity.y = 0.0f;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (buttons[Buttons::PaddleTwoUp])
|
|
||||||
{
|
|
||||||
paddleTwo.velocity.y = -PADDLE_SPEED;
|
|
||||||
}
|
|
||||||
else if (buttons[Buttons::PaddleTwoDown])
|
|
||||||
{
|
|
||||||
paddleTwo.velocity.y = PADDLE_SPEED;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
paddleTwo.velocity.y = 0.0f;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update the paddle positions
|
|
||||||
paddleOne.Update(dt);
|
|
||||||
paddleTwo.Update(dt);
|
|
||||||
|
|
||||||
// Clear the window to black
|
|
||||||
SDL_SetRenderDrawColor(gRenderer, 0x0, 0x0, 0x0, 0xFF);
|
|
||||||
SDL_RenderClear(gRenderer);
|
|
||||||
|
|
||||||
// Set the draw color to be white
|
|
||||||
SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
|
|
||||||
|
|
||||||
// Draw the net
|
|
||||||
for (int y = 0; y < WINDOW_HEIGHT; ++y)
|
|
||||||
{
|
|
||||||
if (y % 5)
|
|
||||||
{
|
|
||||||
SDL_RenderDrawPoint(gRenderer, WINDOW_WIDTH / 2, y);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Draw the ball
|
|
||||||
ball.Draw(gRenderer);
|
|
||||||
|
|
||||||
// Draw the paddles
|
|
||||||
paddleOne.Draw(gRenderer);
|
|
||||||
paddleTwo.Draw(gRenderer);
|
|
||||||
|
|
||||||
// Display the scores
|
|
||||||
playerOneScoreText.Draw();
|
|
||||||
playerTwoScoreText.Draw();
|
|
||||||
|
|
||||||
randomAssSprite.Draw();
|
|
||||||
|
|
||||||
// Present the backbuffer
|
|
||||||
SDL_RenderPresent(gRenderer);
|
|
||||||
|
|
||||||
// Calculate frame time
|
|
||||||
auto stopTime = std::chrono::high_resolution_clock::now();
|
|
||||||
dt = std::chrono::duration<float, std::chrono::milliseconds::period>(stopTime - startTime).count();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Cleanup
|
|
||||||
SDL_DestroyRenderer(gRenderer);
|
|
||||||
SDL_DestroyWindow(gWindow);
|
|
||||||
TTF_CloseFont(scoreFont);
|
|
||||||
SDL_Quit();
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
368
Slang/graphics.h
368
Slang/graphics.h
@ -15,105 +15,329 @@
|
|||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
#include <SDL_image.h>
|
#include <SDL_image.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <chrono>
|
||||||
|
#include <SDL.h>
|
||||||
|
#include <SDL_ttf.h>
|
||||||
|
#include <SDL_image.h>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
using namespace std;
|
int WINDOW_WIDTH = 1280;
|
||||||
|
int WINDOW_HEIGHT = 720;
|
||||||
|
|
||||||
class Parser
|
enum Buttons
|
||||||
|
{
|
||||||
|
PaddleOneUp = 0,
|
||||||
|
PaddleOneDown,
|
||||||
|
PaddleTwoUp,
|
||||||
|
PaddleTwoDown,
|
||||||
|
};
|
||||||
|
|
||||||
|
const float PADDLE_SPEED = 1.0f;
|
||||||
|
|
||||||
|
|
||||||
|
//The window we'll be rendering to
|
||||||
|
SDL_Window* gWindow = NULL;
|
||||||
|
|
||||||
|
//The renderer we'll be rendering to
|
||||||
|
SDL_Renderer* gRenderer = NULL;
|
||||||
|
|
||||||
|
//The surface contained by the window
|
||||||
|
SDL_Surface* gScreenSurface = NULL;
|
||||||
|
|
||||||
|
bool running = true;
|
||||||
|
bool buttons[4] = {};
|
||||||
|
float dt = 0.0f;
|
||||||
|
|
||||||
|
SDL_Surface* loadSurface(std::string path)
|
||||||
|
{
|
||||||
|
//The final optimized image
|
||||||
|
SDL_Surface* optimizedSurface = NULL;
|
||||||
|
|
||||||
|
//Load image at specified path
|
||||||
|
SDL_Surface* loadedSurface = IMG_Load(path.c_str());
|
||||||
|
if (loadedSurface == NULL)
|
||||||
|
{
|
||||||
|
printf("Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//Convert surface to screen format
|
||||||
|
optimizedSurface = SDL_ConvertSurface(loadedSurface, gScreenSurface->format, 0);
|
||||||
|
if (optimizedSurface == NULL)
|
||||||
|
{
|
||||||
|
printf("Unable to optimize image %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
|
||||||
|
}
|
||||||
|
|
||||||
|
//Get rid of old loaded surface
|
||||||
|
SDL_FreeSurface(loadedSurface);
|
||||||
|
}
|
||||||
|
|
||||||
|
return optimizedSurface;
|
||||||
|
}
|
||||||
|
|
||||||
|
class Vec2
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
int Start(int SCREEN_W, int SCREEN_H)
|
Vec2()
|
||||||
|
: x(0.0f), y(0.0f)
|
||||||
|
{}
|
||||||
|
|
||||||
|
Vec2(float x, float y)
|
||||||
|
: x(x), y(y)
|
||||||
|
{}
|
||||||
|
|
||||||
|
Vec2 operator+(Vec2 const& rhs)
|
||||||
{
|
{
|
||||||
//// variable declarations
|
return Vec2(x + rhs.x, y + rhs.y);
|
||||||
//SDL_Window* win = NULL;
|
}
|
||||||
//SDL_Renderer* renderer = NULL;
|
|
||||||
//int w, h; // texture width & height
|
|
||||||
|
|
||||||
//// Initialize SDL.
|
Vec2& operator+=(Vec2 const& rhs)
|
||||||
//if (SDL_Init(SDL_INIT_VIDEO) < 0)
|
{
|
||||||
// return 1;
|
x += rhs.x;
|
||||||
|
y += rhs.y;
|
||||||
|
|
||||||
//// create the window and renderer
|
return *this;
|
||||||
//// note that the renderer is accelerated
|
}
|
||||||
//win = SDL_CreateWindow("Image Loading", 100, 100, SCREEN_W, SCREEN_H, 0);
|
|
||||||
//renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
|
|
||||||
|
|
||||||
//// load our image
|
Vec2 operator*(float rhs)
|
||||||
//SDL_Surface* imgSurface = IMG_Load("circle.png");
|
{
|
||||||
|
return Vec2(x * rhs, y * rhs);
|
||||||
|
}
|
||||||
|
|
||||||
//// main loop
|
float x, y;
|
||||||
//while (1) {
|
};
|
||||||
|
|
||||||
// // event handling
|
class Sprite
|
||||||
// SDL_Event e;
|
{
|
||||||
// if (SDL_PollEvent(&e)) {
|
public:
|
||||||
// if (e.type == SDL_QUIT)
|
Sprite(std::string path, Vec2 position, Vec2 scale, double angle)
|
||||||
// break;
|
: position(position), angle(angle)
|
||||||
// else if (e.type == SDL_KEYUP && e.key.keysym.sym == SDLK_ESCAPE)
|
{
|
||||||
// break;
|
rect.x = static_cast<int>(position.x);
|
||||||
// }
|
rect.y = static_cast<int>(position.y);
|
||||||
|
rect.w = scale.x;
|
||||||
|
rect.h = scale.y;
|
||||||
|
|
||||||
// // clear the screen
|
SDL_Surface* surface = loadSurface(path);
|
||||||
// SDL_RenderClear(renderer);
|
texture = SDL_CreateTextureFromSurface(gRenderer, surface);
|
||||||
// // copy the texture to the rendering context
|
SDL_FreeSurface(surface);
|
||||||
// SDL_RenderCopy(renderer, img, NULL, &texr);
|
}
|
||||||
// // flip the backbuffer
|
|
||||||
// // this means that everything that we prepared behind the screens is actually shown
|
|
||||||
// SDL_RenderPresent(renderer);
|
|
||||||
|
|
||||||
//}
|
void Draw()
|
||||||
|
{
|
||||||
|
rect.y = static_cast<int>(position.y);
|
||||||
|
|
||||||
//SDL_DestroyTexture(img);
|
SDL_RenderCopy(gRenderer, texture, NULL, &rect);
|
||||||
//SDL_DestroyRenderer(renderer);
|
}
|
||||||
//SDL_DestroyWindow(win);
|
|
||||||
|
|
||||||
//return 0;
|
Vec2 position;
|
||||||
|
double angle;
|
||||||
|
std::string path;
|
||||||
|
SDL_Rect rect{};
|
||||||
|
SDL_Texture* texture;
|
||||||
|
};/*
|
||||||
|
|
||||||
|
class PlayerScore
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
PlayerScore(Vec2 position, SDL_Renderer* renderer, TTF_Font* font, SDL_Color scoreColor)
|
||||||
|
: renderer(renderer), font(font)
|
||||||
|
{
|
||||||
|
surface = TTF_RenderText_Solid(font, "0", scoreColor);
|
||||||
|
texture = SDL_CreateTextureFromSurface(renderer, surface);
|
||||||
|
|
||||||
|
int width, height;
|
||||||
|
SDL_QueryTexture(texture, nullptr, nullptr, &width, &height);
|
||||||
|
|
||||||
|
rect.x = static_cast<int>(position.x);
|
||||||
|
rect.y = static_cast<int>(position.y);
|
||||||
|
rect.w = width;
|
||||||
|
rect.h = height;
|
||||||
|
}
|
||||||
|
|
||||||
|
~PlayerScore()
|
||||||
|
{
|
||||||
|
SDL_FreeSurface(surface);
|
||||||
|
SDL_DestroyTexture(texture);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Draw()
|
||||||
|
{
|
||||||
|
SDL_RenderCopy(renderer, texture, nullptr, &rect);
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_Renderer* renderer;
|
||||||
|
TTF_Font* font;
|
||||||
|
SDL_Surface* surface{};
|
||||||
|
SDL_Texture* texture{};
|
||||||
|
SDL_Rect rect{};
|
||||||
|
};*/
|
||||||
|
|
||||||
|
int cleanupGraphics()
|
||||||
|
{
|
||||||
|
// Cleanup
|
||||||
|
SDL_DestroyRenderer(gRenderer);
|
||||||
|
SDL_DestroyWindow(gWindow);
|
||||||
|
SDL_Quit();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int updateLoop()
|
||||||
|
{
|
||||||
|
// Continue looping and processing events until user exits
|
||||||
|
while (running)
|
||||||
|
{
|
||||||
|
auto startTime = std::chrono::high_resolution_clock::now();
|
||||||
|
|
||||||
////Initialize SDL
|
SDL_Event event;
|
||||||
//if (SDL_Init(SDL_INIT_VIDEO) < 0)
|
while (SDL_PollEvent(&event))
|
||||||
|
{
|
||||||
|
if (event.type == SDL_QUIT)
|
||||||
|
{
|
||||||
|
running = false;
|
||||||
|
}
|
||||||
|
else if (event.type == SDL_KEYDOWN)
|
||||||
|
{
|
||||||
|
if (event.key.keysym.sym == SDLK_ESCAPE)
|
||||||
|
{
|
||||||
|
running = false;
|
||||||
|
}
|
||||||
|
else if (event.key.keysym.sym == SDLK_w)
|
||||||
|
{
|
||||||
|
buttons[Buttons::PaddleOneUp] = true;
|
||||||
|
}
|
||||||
|
else if (event.key.keysym.sym == SDLK_s)
|
||||||
|
{
|
||||||
|
buttons[Buttons::PaddleOneDown] = true;
|
||||||
|
}
|
||||||
|
else if (event.key.keysym.sym == SDLK_UP)
|
||||||
|
{
|
||||||
|
buttons[Buttons::PaddleTwoUp] = true;
|
||||||
|
}
|
||||||
|
else if (event.key.keysym.sym == SDLK_DOWN)
|
||||||
|
{
|
||||||
|
buttons[Buttons::PaddleTwoDown] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (event.type == SDL_KEYUP)
|
||||||
|
{
|
||||||
|
if (event.key.keysym.sym == SDLK_w)
|
||||||
|
{
|
||||||
|
buttons[Buttons::PaddleOneUp] = false;
|
||||||
|
}
|
||||||
|
else if (event.key.keysym.sym == SDLK_s)
|
||||||
|
{
|
||||||
|
buttons[Buttons::PaddleOneDown] = false;
|
||||||
|
}
|
||||||
|
else if (event.key.keysym.sym == SDLK_UP)
|
||||||
|
{
|
||||||
|
buttons[Buttons::PaddleTwoUp] = false;
|
||||||
|
}
|
||||||
|
else if (event.key.keysym.sym == SDLK_DOWN)
|
||||||
|
{
|
||||||
|
buttons[Buttons::PaddleTwoDown] = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//if (buttons[Buttons::PaddleOneUp])
|
||||||
//{
|
//{
|
||||||
// printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
|
// paddleOne.velocity.y = -PADDLE_SPEED;
|
||||||
|
//}
|
||||||
|
//else if (buttons[Buttons::PaddleOneDown])
|
||||||
|
//{
|
||||||
|
// paddleOne.velocity.y = PADDLE_SPEED;
|
||||||
//}
|
//}
|
||||||
//else
|
//else
|
||||||
//{
|
//{
|
||||||
// //Create window
|
// paddleOne.velocity.y = 0.0f;
|
||||||
// window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, w, h, SDL_WINDOW_SHOWN);
|
|
||||||
// if (window == NULL)
|
|
||||||
// {
|
|
||||||
// printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
|
|
||||||
// }
|
|
||||||
// else
|
|
||||||
// {
|
|
||||||
// //Get window surface
|
|
||||||
// screenSurface = SDL_GetWindowSurface(window);
|
|
||||||
|
|
||||||
// while (OnUpdate() == 0)
|
|
||||||
// {
|
|
||||||
// //Fill the surface white
|
|
||||||
// SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0xFF, 0x00, 0x00));
|
|
||||||
//
|
|
||||||
// //Update the surface
|
|
||||||
// SDL_UpdateWindowSurface(window);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//}
|
//}
|
||||||
|
|
||||||
////Destroy window
|
//if (buttons[Buttons::PaddleTwoUp])
|
||||||
//SDL_DestroyWindow(window);
|
//{
|
||||||
|
// paddleTwo.velocity.y = -PADDLE_SPEED;
|
||||||
|
//}
|
||||||
|
//else if (buttons[Buttons::PaddleTwoDown])
|
||||||
|
//{
|
||||||
|
// paddleTwo.velocity.y = PADDLE_SPEED;
|
||||||
|
//}
|
||||||
|
//else
|
||||||
|
//{
|
||||||
|
// paddleTwo.velocity.y = 0.0f;
|
||||||
|
//}
|
||||||
|
|
||||||
//Quit SDL subsystems
|
//// Update the paddle positions
|
||||||
SDL_Quit();
|
//paddleOne.Update(dt);
|
||||||
|
//paddleTwo.Update(dt);
|
||||||
|
|
||||||
|
// Clear the window to black
|
||||||
|
SDL_SetRenderDrawColor(gRenderer, 0x0, 0x0, 0x0, 0xFF);
|
||||||
|
SDL_RenderClear(gRenderer);
|
||||||
|
|
||||||
|
// Set the draw color to be white
|
||||||
|
SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
|
||||||
|
|
||||||
|
// Draw the net
|
||||||
|
for (int y = 0; y < WINDOW_HEIGHT; ++y)
|
||||||
|
{
|
||||||
|
if (y % 5)
|
||||||
|
{
|
||||||
|
SDL_RenderDrawPoint(gRenderer, WINDOW_WIDTH / 2, y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//// Draw the ball
|
||||||
|
//ball.Draw(gRenderer);
|
||||||
|
|
||||||
|
//// Draw the paddles
|
||||||
|
//paddleOne.Draw(gRenderer);
|
||||||
|
//paddleTwo.Draw(gRenderer);
|
||||||
|
|
||||||
|
//// Display the scores
|
||||||
|
//playerOneScoreText.Draw();
|
||||||
|
//playerTwoScoreText.Draw();
|
||||||
|
|
||||||
|
//randomAssSprite.Draw();
|
||||||
|
|
||||||
|
// Present the backbuffer
|
||||||
|
SDL_RenderPresent(gRenderer);
|
||||||
|
|
||||||
|
// Calculate frame time
|
||||||
|
auto stopTime = std::chrono::high_resolution_clock::now();
|
||||||
|
dt = std::chrono::duration<float, std::chrono::milliseconds::period>(stopTime - startTime).count();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int OnUpdate()
|
int initGraphics(std::string windowTitle, int width, int height)
|
||||||
{
|
{
|
||||||
ExecuteFunction("Update", vector<boost::any>());
|
WINDOW_WIDTH = width;
|
||||||
//cout << "update" << endl;
|
WINDOW_HEIGHT = height;
|
||||||
return 0;
|
|
||||||
}
|
// Initialize SDL components
|
||||||
};
|
SDL_Init(SDL_INIT_VIDEO);
|
||||||
|
TTF_Init();
|
||||||
|
|
||||||
|
gWindow = SDL_CreateWindow(windowTitle.c_str(), 40, 40, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN || SDL_WINDOW_RESIZABLE);
|
||||||
|
gRenderer = SDL_CreateRenderer(gWindow, -1, 0);
|
||||||
|
|
||||||
|
//Get window surface
|
||||||
|
gScreenSurface = SDL_GetWindowSurface(gWindow);
|
||||||
|
|
||||||
|
//Sprite randomAssSprite(
|
||||||
|
// Vec2((WINDOW_WIDTH / 2.0f) - (100 / 2.0f), (WINDOW_HEIGHT / 2.0f) - (100 / 2.0f)),
|
||||||
|
// Vec2(100, 100),
|
||||||
|
// 0,
|
||||||
|
// "./circle.png");
|
||||||
|
|
||||||
|
updateLoop();
|
||||||
|
|
||||||
|
cleanupGraphics();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
func Main(input, in)
|
func Main(input, in)
|
||||||
{
|
{
|
||||||
|
|
||||||
print "PI is: " + PI
|
print "PI is: " + PI
|
||||||
int x = 1
|
int x = 1
|
||||||
print x
|
print x
|
||||||
@ -18,9 +17,9 @@ func Main(input, in)
|
|||||||
|
|
||||||
x += 1
|
x += 1
|
||||||
}
|
}
|
||||||
|
Vec2 aa = Vec2(0, 0)
|
||||||
NewCircle(0, 0, 5)
|
Sprite thisisasprite = CPP.Graphics.Sprite("circle.png", aa, aa, 0)
|
||||||
CPP.Graphics.Init(64, 64)
|
//CPP.Graphics.Init("This is a pong game", 64, 64)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Update(input)
|
func Update(input)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user