Giter Club home page Giter Club logo

twinklebeardev-lessons's Introduction

TwinklebearDev-Lessons

Source code and assets for the TwinklebearDev SDL 2.0 Lessons. The full lesson text is available at my site.

Lesson Index:

  • Lesson 0: Setting up SDL:
    • A simple program to make sure you've set up SDL correctly
  • Lesson 1: Hello World:
    • The standard Hello World program, for SDL 2.0
  • Lesson 2: Don't Put Everything in Main
    • An introduction to creating some functions to help us with writing better and reusable code.
  • Lesson 3: SDL Extension Libraries
    • An introduction to the various extension libraries that are available for SDL in this lesson we cover usage of SDL_image to load non-BMP images
  • Lesson 4: Handling Events
    • An introduction to using SDL's event system to get user input from the window, mouse and keyboard
  • Lesson 5: Clipping Sprite Sheets
    • An introduction to selecting specific subsets of an image sheet, ie. sprite sheet, that we want to draw
  • Lesson 6: True Type Fonts with SDL_ttf
    • An introduction to using the SDL_ttf extension library to render true type fonts

Other Language Bindings

twinklebeardev-lessons's People

Contributors

amdmi3 avatar chuckination avatar krizalys avatar twinklebear avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

twinklebeardev-lessons's Issues

Plans for Tutorial 7 and 8?

Hi I found your tutorials for SDL 2.0 really informative and a great starting point for using SDL. I noticed however that you only have write-ups for tutorials 1-6 (7 and 8 are left out).

Do you have any plans to create tutorial write-ups for these two? If so, do you have an approximate timeframe of when you plan to write them?

"os << msg" in logSDLError does not work in Visual Studios

I was following lesson 2 where logSDLError() was to be implemented. The tutorial showed:

void logSDLError(std::ostream &os, const std::string &msg){
    os << msg << " error: " << SDL_GetError() << std::endl;
}

But on Visual Studios 2012 express edition, the << between os and msg gets underlined red and the compiler complains that << cannot take in msg. Here's what Visual Studios said:

error C2679: binary '<<' : no operator found which takes a right-hand operand 
of type 'const std::string' (or there is no acceptable conversion)  

Fortunately I found a workaround. The string msg instance should call c_str() to get the string in C-String form, like:

os << msg.c_str() << " error: " << SDL_GetError() << std::endl;

Everything worked after that.

Copy resources if lessons not built in situ

I've run into situations where building a project in the same folder as the source is not practical. What I have found to be the best practice is to make a separate directory and invoke all necessary build commands in that folder. Clean up then becomes a matter of removing the created directory. This is especially useful when wanting to refresh/reset CMake's cache values.

However, current project do not allow for this practice. The resources folder res would have to be manually copied over. I only discovered that by accident and digging. Others may not be so savvy.

To allow for alternate build location, I would like to suggest the code below be appended to the end of the CMakeLists.txt file at directory root right after the line add_subdirectory(Lesson6):

if ( NOT ${PROJECT_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_BINARY_DIR})
    file(COPY "${PROJECT_SOURCE_DIR}/res" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
endif()

CMake file(COPY...) command will do the heavy lifting to copy the directory from source to destination. The if statement is there to prevent overwriting the res directory if in-place build is being done.

Lesson 0 crashes on Visual Studio

After following the steps for setting up SDL in Visual Studio, I'm able to build the executable but when I run it fails with a message saying it can't find SDL2.dll

On Lesson 1, the window does not show up

I tried the tutorial Lesson 1. I successfully compiled the file, but when running, it did not show any window; all that happened was that the terminal stopped for exactly three seconds and then the program ended with exit code 0.

I googled and found a similar issue on StackOverflow: SDL Window Not Showing Up at all. According to the answer, it may be the problem that SDL_Delay prevents the window from opening. I tried two source code on the page, one from the question and the other from the answer, then the former does not show the window, while the latter did. So I guess the reason Lesson 1 did not work is the same to that? Maybe it needs to be rewritten.

Also, if you don't mind, please tell me why "SDL_Delay [blocks] the window gets a chance to show"? I'm completely new to SDL, and I want to understand how does it work, what is going on under the hood.

System Information:

$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.14.6
BuildVersion:   18G2022
$ g++ --version
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
$ brew info sdl2
sdl2: stable 2.0.10 (bottled), HEAD
Low-level access to audio, keyboard, mouse, joystick, and graphics
https://www.libsdl.org/
/usr/local/Cellar/sdl2/2.0.10 (87 files, 4.6MB) *
  Poured from bottle on 2019-09-03 at 09:45:53
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/sdl2.rb
==> Options
--HEAD
        Install HEAD version
==> Analytics
install: 53,946 (30 days), 163,725 (90 days), 730,192 (365 days)
install-on-request: 6,346 (30 days), 19,145 (90 days), 82,627 (365 days)
build-error: 0 (30 days)

The source code I wrote, which did not show a window:

#include <iostream>
#include <SDL.h>

int main(int argc, const char **argv){

if (SDL_Init(SDL_INIT_VIDEO) != 0){
	std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
	return 1;
}

SDL_Window *win = SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN);
if (win == nullptr){
	std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
	SDL_Quit();
	return 1;
}

SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (ren == nullptr){
	SDL_DestroyWindow(win);
	std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;
	SDL_Quit();
	return 1;
}

std::string imagePath = "/path/to/hello.bmp";
SDL_Surface *bmp = SDL_LoadBMP(imagePath.c_str());
if (bmp == nullptr){
	SDL_DestroyRenderer(ren);
	SDL_DestroyWindow(win);
	std::cout << "SDL_LoadBMP Error: " << SDL_GetError() << std::endl;
	SDL_Quit();
	return 1;
}

SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, bmp);
SDL_FreeSurface(bmp);
if (tex == nullptr){
	SDL_DestroyRenderer(ren);
	SDL_DestroyWindow(win);
	std::cout << "SDL_CreateTextureFromSurface Error: " << SDL_GetError() << std::endl;
	SDL_Quit();
	return 1;
}

//A sleepy rendering loop, wait for 3 seconds and render and present the screen each time
for (int i = 0; i < 10; ++i){
 	//First clear the renderer
 	SDL_RenderClear(ren);
 	//Draw the texture
 	SDL_RenderCopy(ren, tex, NULL, NULL);
 	//Update the screen
 	SDL_RenderPresent(ren);
	//Take a quick break after all that hard work
	SDL_Delay(1000);
}

SDL_DestroyTexture(tex);
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
SDL_Quit();

	return 0;
}

I know I'm pedantic, but I'm a programmer...

In the 1st paragraph of lesson 4, you wrote:

To read events SDL provides the SDL_Event structure

SDL_Event is actually a union, not a struct.
Would you like to fix it, 7 years later? haha

it made kwin crash

I run https://github.com/Twinklebear/TwinklebearDev-Lessons/blob/master/Lesson1/src/main.cpp,
and kwin crash:

QXcbConnection: XCB error: 3 (BadWindow), sequence: 49027, resource id: 115343374, major code: 18 (ChangeProperty), minor code: 0
QXcbConnection: XCB error: 3 (BadWindow), sequence: 49028, resource id: 115343374, major code: 18 (ChangeProperty), minor code: 0
QXcbConnection: XCB error: 3 (BadWindow), sequence: 49034, resource id: 115343374, major code: 18 (ChangeProperty), minor code: 0
QXcbConnection: XCB error: 3 (BadWindow), sequence: 49035, resource id: 115343374, major code: 20 (GetProperty), minor code: 0
QXcbConnection: XCB error: 3 (BadWindow), sequence: 49037, resource id: 115343374, major code: 18 (ChangeProperty), minor code: 0
QXcbConnection: XCB error: 3 (BadWindow), sequence: 49038, resource id: 115343374, major code: 42 (SetInputFocus), minor code: 0
QXcbConnection: XCB error: 3 (BadWindow), sequence: 49039, resource id: 115343374, major code: 25 (SendEvent), minor code: 0
QXcbConnection: XCB error: 3 (BadWindow), sequence: 49044, resource id: 115343374, major code: 12 (ConfigureWindow), minor code: 0
QXcbConnection: XCB error: 3 (BadWindow), sequence: 49045, resource id: 115343374, major code: 25 (SendEvent), minor code: 0
QXcbConnection: XCB error: 3 (BadWindow), sequence: 49051, resource id: 115343374, major code: 18 (ChangeProperty), minor code: 0
QXcbConnection: XCB error: 3 (BadWindow), sequence: 49052, resource id: 115343374, major code: 18 (ChangeProperty), minor code: 0
QXcbConnection: XCB error: 3 (BadWindow), sequence: 49053, resource id: 115343374, major code: 20 (GetProperty), minor code: 0
QXcbConnection: XCB error: 3 (BadWindow), sequence: 49113, resource id: 115343374, major code: 20 (GetProperty), minor code: 0
QXcbConnection: XCB error: 3 (BadWindow), sequence: 49114, resource id: 115343374, major code: 18 (ChangeProperty), minor code: 0
QXcbConnection: XCB error: 3 (BadWindow), sequence: 49119, resource id: 115343374, major code: 15 (QueryTree), minor code: 0
QXcbConnection: XCB error: 3 (BadWindow), sequence: 49182, resource id: 113249506, major code: 3 (GetWindowAttributes), minor code: 0
QXcbConnection: XCB error: 9 (BadDrawable), sequence: 49183, resource id: 113249506, major code: 14 (GetGeometry), minor code: 0
QXcbConnection: XCB error: 3 (BadWindow), sequence: 49209, resource id: 113249504, major code: 15 (QueryTree), minor code: 0
QXcbConnection: XCB error: 9 (BadDrawable), sequence: 49212, resource id: 113249504, major code: 55 (CreateGC), minor code: 0
QXcbConnection: XCB error: 13 (BadGC), sequence: 49213, resource id: 113249509, major code: 59 (SetClipRectangles), minor code: 0
QXcbConnection: XCB error: 9 (BadDrawable), sequence: 49214, resource id: 113249504, major code: 130 (Unknown), minor code: 3
QXcbConnection: XCB error: 13 (BadGC), sequence: 49215, resource id: 113249509, major code: 56 (ChangeGC), minor code: 0
QXcbConnection: XCB error: 13 (BadGC), sequence: 49216, resource id: 113249509, major code: 59 (SetClipRectangles), minor code: 0
QXcbConnection: XCB error: 9 (BadDrawable), sequence: 49217, resource id: 113249504, major code: 130 (Unknown), minor code: 3
QXcbConnection: XCB error: 13 (BadGC), sequence: 49218, resource id: 113249509, major code: 56 (ChangeGC), minor code: 0
kwin_x11: Couldn't find current GLX or EGL context.

License

without a license we are not even allowed to run or copy your code :(( its all rights reserved for u ,
u should consider adding one ( Public Domain, MIT, ...... )

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.