Bart van Strien avatar Bart van Strien committed 775c1c8

Implement more basic graphics functions

Comments (0)

Files changed (7)

src/modules/graphics/sdl/Graphics.cpp

 // LOVE
 #include "Graphics.h"
 
-#include <window/sdl/Window.h>
+// SDL
+#include "SDL_gfxPrimitives.h"
 
 namespace love
 {
 namespace sdl
 {
 	Graphics::Graphics()
+		: surface(0)
 	{
-		currentWindow = love::window::sdl::Window::getSingleton();
+		currentWindow = love::window::sdl::Window::getSDLSingleton();
+		reset();
 	}
 
 	Graphics::~Graphics()
 
 	bool Graphics::setMode(int width, int height, bool fullscreen, bool vsync, int fsaa)
 	{
-		return currentWindow->setWindow(width, height, fullscreen, vsync, fsaa);
+		bool success = currentWindow->setWindow(width, height, fullscreen, vsync, fsaa);
+		if (success)
+			surface = currentWindow->getSurface();
+		return success;
 	}
 
 	void Graphics::getMode(int &width, int &height, bool &fullscreen, bool &vsync, int &fsaa)
 	{
 		return currentWindow->getHeight();
 	}
+
+	void Graphics::reset()
+	{
+		foregroundColor.set(255, 255, 255, 255);
+		backgroundColor.set(0, 0, 0, 255);
+	}
+
+	void Graphics::clear()
+	{
+		if (surface == 0)
+			return;
+		boxRGBA(surface, 0, 0, getWidth(), getHeight(),
+				backgroundColor.r, backgroundColor.g, backgroundColor.b, backgroundColor.a);
+	}
+
+	void Graphics::present()
+	{
+		currentWindow->swapBuffers();
+	}
+
+	void Graphics::setCaption(const char *caption)
+	{
+		currentWindow->setWindowTitle(caption);
+	}
+
+	const char *Graphics::getCaption() const
+	{
+		return currentWindow->getWindowTitle().c_str();
+	}
+
+	void Graphics::setColor(const Color &c)
+	{
+		foregroundColor = c;
+	}
+
+	Color Graphics::getColor()
+	{
+		return foregroundColor;
+	}
+
+	void Graphics::setBackgroundColor(const Color &c)
+	{
+		backgroundColor = c;
+	}
+
+	Color Graphics::getBackgroundColor()
+	{
+		return backgroundColor;
+	}
 } // sdl
 } // graphics
 } // love

src/modules/graphics/sdl/Graphics.h

 #include <image/Image.h>
 #include <image/ImageData.h>
 
-#include <window/Window.h>
+#include <window/sdl/Window.h>
 
 #include "Image.h"
 
+// SDL
+#include <SDL.h>
+
 namespace love
 {
 namespace graphics
 	class Graphics : public love::graphics::Graphics
 	{
 	private:
-		love::window::Window *currentWindow;
+		love::window::sdl::Window *currentWindow;
+		Color foregroundColor, backgroundColor;
+		SDL_Surface *surface;
 
 	public:
 		Graphics();
 		int getWidth();
 		int getHeight();
 
-/*		void reset();
+		void reset();
 		void clear();
 		void present();
 
 		void setCaption(const char *caption);
-		const char *getCaption();
+		const char *getCaption() const;
 
-		Image *newImage(love::filesystem::File *file);
+/*		Image *newImage(love::filesystem::File *file);
 		Image *newImage(love::image::ImageData *data);
-		Quad *newQuad(float x, float y, float w, float h, float sw, float sh);
+		Quad *newQuad(float x, float y, float w, float h, float sw, float sh);*/
 
 		void setColor(const Color &c);
 		Color getColor();
 		void setBackgroundColor(const Color &c);
 		Color getBackgroundColor();
 
-		void point(float x, float y);
+/*		void point(float x, float y);
 		void triangle(DrawMode mode, float x1, float y1, float x2, float y2, float x3, float y3);
 		void rectangle(DrawMode mode, float x, float y, float w, float h);
 		void quad(DrawMode mode, float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4);

src/modules/graphics/sdl/wrap_Graphics.cpp

 		return 1;
 	}
 
+	int w_reset(lua_State *L)
+	{
+		instance->reset();
+		return 0;
+	}
+
+	int w_clear(lua_State *L)
+	{
+		instance->clear();
+		return 0;
+	}
+
+	int w_present(lua_State *L)
+	{
+		instance->present();
+		return 0;
+	}
+
+	int w_setCaption(lua_State *L)
+	{
+		const char *caption = luaL_checkstring(L, 1);
+		instance->setCaption(caption);
+		return 0;
+	}
+
+	int w_getCaption(lua_State *L)
+	{
+		lua_pushstring(L, instance->getCaption());
+		return 1;
+	}
+
+/*	int w_newImage(lua_State *L)
+	{
+		return 0;
+	}
+
+	int w_newQuad(lua_State *L)
+	{
+		return 0;
+	}*/
+
+	int w_setColor(lua_State *L)
+	{
+		int r = luaL_checkint(L, 1);
+		int g = luaL_checkint(L, 2);
+		int b = luaL_checkint(L, 3);
+		int a = luaL_optint(L, 4, 255);
+		Color c(r, g, b, a);
+		instance->setColor(c);
+		return 0;
+	}
+
+	int w_getColor(lua_State *L)
+	{
+		Color c = instance->getColor();
+		lua_pushinteger(L, c.r);
+		lua_pushinteger(L, c.g);
+		lua_pushinteger(L, c.b);
+		lua_pushinteger(L, c.a);
+		return 4;
+	}
+
+	int w_setBackgroundColor(lua_State *L)
+	{
+		int r = luaL_checkint(L, 1);
+		int g = luaL_checkint(L, 2);
+		int b = luaL_checkint(L, 3);
+		int a = luaL_optint(L, 4, 255);
+		Color c(r, g, b, a);
+		instance->setBackgroundColor(c);
+		return 0;
+	}
+
+	int w_getBackgroundColor(lua_State *L)
+	{
+		Color c = instance->getBackgroundColor();
+		lua_pushinteger(L, c.r);
+		lua_pushinteger(L, c.g);
+		lua_pushinteger(L, c.b);
+		lua_pushinteger(L, c.a);
+		return 4;
+	}
+
+/*	int w_point(lua_State *L)
+	{
+		return 0;
+	}
+
+	int w_triangle(lua_State *L)
+	{
+		return 0;
+	}
+
+	int w_rectangle(lua_State *L)
+	{
+		return 0;
+	}
+
+	int w_quad(lua_State *L)
+	{
+		return 0;
+	}
+
+	int w_circle(lua_State *L)
+	{
+		return 0;
+	}
+
+	int w_draw(lua_State *L)
+	{
+		return 0;
+	}
+
+	int w_drawq(lua_State *L)
+	{
+		return 0;
+	}*/
+
 	// List of functions to wrap.
 	static const luaL_Reg functions[] = {
 		{ "setMode", w_setMode },
 		{ "isCreated", w_isCreated },
 		{ "getWidth", w_getWidth },
 		{ "getHeight", w_getHeight },
+		{ "reset", w_reset },
+		{ "clear", w_clear },
+		{ "present", w_present },
+		{ "setCaption", w_setCaption },
+		{ "getCaption", w_getCaption },
+/*		{ "newImage", w_newImage },
+		{ "newQuad", w_newQuad },*/
+		{ "setColor", w_setColor },
+		{ "getColor", w_getColor },
+		{ "setBackgroundColor", w_setBackgroundColor },
+		{ "getBackgroundColor", w_getBackgroundColor },
+/*		{ "point", w_point },
+		{ "triangle", w_triangle },
+		{ "rectangle", w_rectangle },
+		{ "quad", w_quad },
+		{ "circle", w_circle },
+		{ "draw", w_draw },
+		{ "drawq", w_drawq },*/
 		{ 0, 0 }
 	};
 

src/modules/graphics/sdl/wrap_Graphics.h

 	int w_isCreated(lua_State *L);
 	int w_getWidth(lua_State *L);
 	int w_getHeight(lua_State *L);
-/*	int w_reset(lua_State *L);
+	int w_reset(lua_State *L);
 	int w_clear(lua_State *L);
 	int w_present(lua_State *L);
 	int w_setCaption(lua_State *L);
 	int w_getCaption(lua_State *L);
-	int w_newImage(lua_State *L);
-	int w_newQuad(lua_State *L);
+/*	int w_newImage(lua_State *L);
+	int w_newQuad(lua_State *L);*/
 	int w_setColor(lua_State *L);
 	int w_getColor(lua_State *L);
 	int w_setBackgroundColor(lua_State *L);
 	int w_getBackgroundColor(lua_State *L);
-	int w_point(lua_State *L);
+/*	int w_point(lua_State *L);
 	int w_triangle(lua_State *L);
 	int w_rectangle(lua_State *L);
 	int w_quad(lua_State *L);
-	int w_circle(lua_State *L);*/
+	int w_circle(lua_State *L);
+	int w_draw(lua_State *L);
+	int w_drawq(lua_State *L);*/
 	extern "C" LOVE_EXPORT int luaopen_love_graphics(lua_State *L);
 } // sdl
 } // graphics

src/modules/window/Window.h

 
 		virtual bool isCreated() = 0;
 
-		virtual void setWindowTitle(std::string &title) = 0;
-		virtual std::string getWindowTitle() = 0;
+		virtual void setWindowTitle(const std::string &title) = 0;
+		virtual const std::string &getWindowTitle() = 0;
 
 		virtual bool setIcon(love::image::ImageData *imgd) = 0;
 

src/modules/window/sdl/Window.cpp

 namespace sdl
 {
 	Window::Window()
-		: windowTitle(""), created(false), mouseVisible(true)
+		: windowTitle(""), created(false), mouseVisible(true), surface(0)
 	{
 		// Window should be centered.
 		SDL_putenv(const_cast<char *>("SDL_VIDEO_CENTERED=center"));
 		Uint32 sdlflags = fullscreen ? SDL_FULLSCREEN : 0;
 
 		// Have SDL set the video mode.
-		if (SDL_SetVideoMode(width, height, 32, sdlflags) == 0)
+		surface = SDL_SetVideoMode(width, height, 32, sdlflags);
+		if (surface == 0)
 		{
 			bool failed = true;
 			if (fsaa > 0)
 			{
 				// FSAA might have failed, disable it and try again
 				//SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0);
-				failed = SDL_SetVideoMode(width, height, 32, sdlflags) == 0;
+				surface = SDL_SetVideoMode(width, height, 32, sdlflags);
+				failed = (surface == 0);
 				if (failed)
 				{
 					// There might be no FSAA at all
 					//SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0);
-					failed = SDL_SetVideoMode(width, height, 32, sdlflags) == 0;
+					surface = SDL_SetVideoMode(width, height, 32, sdlflags);
+					failed = (surface == 0);
 				}
 			}
 			if (failed)
 		return created;
 	}
 
-	void Window::setWindowTitle(std::string &title)
+	void Window::setWindowTitle(const std::string &title)
 	{
 		windowTitle = title;
 		SDL_WM_SetCaption(windowTitle.c_str(), 0);
 	}
 
-	std::string Window::getWindowTitle()
+	const std::string &Window::getWindowTitle()
 	{
-		// not a reference
-		// because we want this untouched
-		// const std::string& might be an option
 		return windowTitle;
 	}
 
 	void Window::swapBuffers()
 	{
 		//SDL_GL_SwapBuffers();
+		if (surface)
+			SDL_Flip(surface);
 	}
 
 	bool Window::hasFocus()
 		return singleton;
 	}
 
+	Window *Window::getSDLSingleton()
+	{
+		if (!singleton)
+			singleton = new Window();
+		else
+			singleton->retain();
+
+		return (Window*) singleton;
+	}
+
+	SDL_Surface *Window::getSurface()
+	{
+		return surface;
+	}
+
 	const char *Window::getName() const
 	{
 		return "love.window.sdl";

src/modules/window/sdl/Window.h

 // LOVE
 #include <window/Window.h>
 
+// SDL
+#include <SDL.h>
+
 namespace love
 {
 namespace window
 		} currentMode;
 		bool created;
 		bool mouseVisible;
+		SDL_Surface *surface;
 
 	public:
 		Window();
 
 		bool isCreated();
 
-		void setWindowTitle(std::string &title);
-		std::string getWindowTitle();
+		void setWindowTitle(const std::string &title);
+		const std::string &getWindowTitle();
 
 		bool setIcon(love::image::ImageData *imgd);
 
 
 		static love::window::Window *getSingleton();
 
+		// SDL additions
+		static Window *getSDLSingleton();
+		SDL_Surface *getSurface();
+
 		const char *getName() const;
 	}; // Window
 } // sdl
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.