From cdc5d643bb3256c50dd9787873b314c80e8f407e Mon Sep 17 00:00:00 2001 From: Richard Knight Date: Tue, 25 Aug 2020 16:14:43 +0100 Subject: initial --- .gitignore | 1 + CMakeLists.txt | 178 ++++++++++++++++++++++++++ README.md | 62 +++++++++ cmake/Modules/FindCsound.cmake | 29 +++++ cmake/Modules/FindSDL2.cmake | 173 +++++++++++++++++++++++++ examples/test.csd | 57 +++++++++ src/opcodes.cpp | 278 +++++++++++++++++++++++++++++++++++++++++ src/tests.cpp | 171 +++++++++++++++++++++++++ 8 files changed, 949 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 README.md create mode 100644 cmake/Modules/FindCsound.cmake create mode 100644 cmake/Modules/FindSDL2.cmake create mode 100644 examples/test.csd create mode 100644 src/opcodes.cpp create mode 100644 src/tests.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..567609b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build/ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..f88e6a1 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,178 @@ +project("csound_sdl") + +cmake_minimum_required(VERSION 3.8) + +set(APIVERSION "6.0") + +# Release or Debug +set(CMAKE_BUILD_TYPE "Release") + +# force make to print the command lines +set(CMAKE_VERBOSE_MAKEFILE on) + +# path to Csound cmake module +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/") + +# set compilation flags +set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-std=c++11 -fpermissive -fPIC -w -DUSE_DOUBLE -DB64BIT") + +# options +option(USE_LIB64 "Set to on to set installation dir for libs to lib64" OFF) +option(USE_DOUBLE "Use doubles for audio calculations" ON) +option(CPP11 "c++11" ON) + +set(BUILDING_CSOUND_PLUGINS ON) + +# ---------------------------------------------- + +include(FindCsound) +include(FindSDL2) + +include(CheckCCompilerFlag) +include(CheckCXXCompilerFlag) + +# ----------------------------------------------- + +function(addflag flag flagname) + check_c_compiler_flag(${flag} ${flagname}) + if (${flagname}) + # message(STATUS "Setting C flag ${flag}") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}" PARENT_SCOPE) + endif() + check_cxx_compiler_flag(${flag} CXX_${flagname}) + if (CXX_${flagname}) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}" PARENT_SCOPE) + endif() +endfunction(addflag) + + +MACRO(SUBDIRLIST result curdir) + FILE(GLOB children RELATIVE ${curdir} ${curdir}/*) + SET(dirlist "") + FOREACH(child ${children}) + message(STATUS "looking at ${child}") + IF(IS_DIRECTORY ${curdir}/${child}) + LIST(APPEND dirlist ${child}) + ENDIF() + ENDFOREACH() + SET(${result} ${dirlist}) +ENDMACRO() + + +MACRO(ADD_ALL_SUBDIRECTORIES directory) + subdirlist(SUBDIRS ${directory}) + message(STATUS "Found subdirs: ${SUBDIRS}") + foreach(SUBDIR ${SUBDIRS}) + set(thissubdir "${directory}/${SUBDIR}") + if(EXISTS "${directory}/${SUBDIR}/CMakeLists.txt") + message(STATUS "Adding subdir: ${thissubdir}") + add_subdirectory(${directory}/${SUBDIR}) + else() + message(WARNING "Skipping ${directory}/${SUBDIR} because no CMakeLists.txt file was found") + endif() + endforeach() +ENDMACRO() + + + +# set optimization flags +if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_CLANG) + add_definitions(-fvisibility=hidden) + if(NATIVE) + add_definitions(-march=native) + endif() + + include(CheckCCompilerFlag) + include(CheckCXXCompilerFlag) + + addflag(-msse HAS_SSE) + addflag(-msse2 HAS_SSE2) + addflag(-mfgpath=sse HAS_FPMATH_SSE) + +endif() + +if(MINGW) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mstackrealign") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mstackrealign") +endif() + +addflag(-ftree-vectorize HAS_TREE_VECTORIZE) +addflag(-ffast-math HAS_FAST_MATH) +addflag(-fomit-frame-pointer HAS_OMIT_FRAME_POINTER) + + +# ------------------------------------------------------------------- + +set(CS_FRAMEWORK_DEST "~/Library/Frameworks") + + +if(USE_LIB64) + set(LIBRARY_INSTALL_DIR "lib64") + add_definitions("-DLIB64") +else() + set(LIBRARY_INSTALL_DIR "lib") +endif() + +message(STATUS "LIBRARY INSTALL DIR: ${LIBRARY_INSTALL_DIR}") + +# ------------------------------------------------------------------- + + +if(USE_DOUBLE) + message(STATUS ">>> using doubles") + + if(APPLE) + set(CSOUNDLIB "CsoundLib64") + set(PLUGIN_INSTALL_DIR "${CS_FRAMEWORK_DEST}/${CSOUNDLIB}.framework/Versions/${APIVERSION}/Resources/Opcodes64") + else() + set(CSOUNDLIB "csound64") + set(PLUGIN_INSTALL_DIR "${LIBRARY_INSTALL_DIR}/csound/plugins64-${APIVERSION}") + endif() +else() + message(STATUS ">>> not using doubles") + if(APPLE) + set(CSOUNDLIB "CsoundLib") + set(PLUGIN_INSTALL_DIR "${CS_FRAMEWORK_DEST}/${CSOUNDLIB}.framework/Versions/${APIVERSION}/Resources/Opcodes") + else() + set(CSOUNDLIB "csound") + set(PLUGIN_INSTALL_DIR "${LIBRARY_INSTALL_DIR}/csound/plugins-${APIVERSION}") + endif() +endif() + + +# ------------------------------------------------------------------- + +# Csound opcode build +find_package(Csound) + + +set(BUILD_PLUGINS_DIR ${CMAKE_CURRENT_BINARY_DIR}) + +if(NOT CSOUND_FOUND) + message(FATAL_ERROR "Csound installation not found") +endif() + +if(NOT SDL2_FOUND) + message(FATAL_ERROR "SDL2 cannot be found") +endif() + + +set(CPPFILES src/opcodes.cpp) + + + +include_directories(${CSOUND_INCLUDE_DIRS}) +include_directories(include) + + +link_libraries(${SDL2_LIBRARY}) +include_directories(${SDL2_INCLUDE_DIR}) + +add_library(sdl SHARED ${CPPFILES}) + +set_target_properties(sdl PROPERTIES + RUNTIME_OUTPUT_DIRECTORY ${BUILD_PLUGINS_DIR} + LIBRARY_OUTPUT_DIRECTORY ${BUILD_PLUGINS_DIR}) + +install(TARGETS sdl LIBRARY DESTINATION "${PLUGIN_INSTALL_DIR}" ) + diff --git a/README.md b/README.md new file mode 100644 index 0000000..ef0aecc --- /dev/null +++ b/README.md @@ -0,0 +1,62 @@ +## Introduction +csound-sdl provides a simple interface for (currently quite) low level SDL calls. At current only basic shapes are available in order to investigate the possibilities and efficiency of using SDL from within Csound. An example is provided in the examples directory which utilises all current opcodes. + + +## Requirements + - Csound libraries + - Cmake + - LibSDL2 + + +## Building +Create a build directory at the top of the source tree, execute *cmake ..*, *make* and optionally *make install* as root. If the latter is not used/possible then the resulting libsdl.so can be used with the *--opcode-lib* flag in Csound. eg: + + cd csound-sdl + mkdir build && cd build + cmake .. + make && sudo make install + + +## Opcode overview +### sdlinit +*ihandle, kmousetrigger, kmousex, kmousey sdlinit Swindowname, iwidth, iheight, ifps* +Initialise the SDL window. + - ihandle: the handle to be used by further SDL opcodes to write to that window + - kmousetrigger: fires when mouse button is clicked in the window + - kmousex: reports mouse X position in window, normalised from 0 to 1 + - kmousey: reports mouse Y position in window, normalised from 0 to 1 + + - Swindowname: the title given to the window + - iwidth: width of window in pixels + - iheight: height of window in pixels + - ifps: frames per second of rendering + +### sdlcolour +*ihandle sdlcolour kred, kgreen, kblue, kalpha* +Set a colour to be used in later drawing. + - ihandle: the handle for the colour to be used by further SDL opcodes + + - kred: red amount with range 0 to 255 + - kgreen: green amount with range 0 to 255 + - kblue: blue amount with range 0 to 255 + - kalpha: alpha amount with range 0 to 255 + +### sdlrect +*sdlrect iwindow, icolour, kx, ky, kwidth, kheight* +Create a rectangle. + - iwindow: the window handle from sdlinit + - icolour: the colour handle from sdlcolour + - kx: X position + - ky: Y position + - kwidth: width + - kheight: height + +## sdlline +*sdlline iwindow, icolour, kx1, ky1, kx2, ky2* +Create a line. + - iwindow: the window handle from sdlinit + - icolour: the colour handle from sdlcolour + - kx1: X position of start point + - ky1: Y position of start point + - kx2: X position of end point + - ky2: Y position of end point diff --git a/cmake/Modules/FindCsound.cmake b/cmake/Modules/FindCsound.cmake new file mode 100644 index 0000000..e55b269 --- /dev/null +++ b/cmake/Modules/FindCsound.cmake @@ -0,0 +1,29 @@ +# Try to find the Csound library. +# Once done this will define: +# CSOUND_FOUND - System has the Csound library +# CSOUND_INCLUDE_DIRS - The Csound include directories. +# CSOUND_LIBRARIES - The libraries needed to use the Csound library. + +if(APPLE) +find_path(CSOUND_INCLUDE_DIR csound.h HINTS /Library/Frameworks/CsoundLib64.framework/Headers +"$ENV{HOME}/Library/Frameworks/CsoundLib64.framework/Headers") +else() +find_path(CSOUND_INCLUDE_DIR csound.h PATH_SUFFIXES csound) +endif() + +if(APPLE) +find_library(CSOUND_LIBRARY NAMES CsoundLib64 HINTS /Library/Frameworks/CsoundLib64.framework/ +"$ENV{HOME}/Library/Frameworks/CsoundLib64.framework") +else() +find_library(CSOUND_LIBRARY NAMES csound64 csound) +endif() + +include(FindPackageHandleStandardArgs) +# handle the QUIETLY and REQUIRED arguments and set CSOUND_FOUND to TRUE +# if all listed variables are TRUE +find_package_handle_standard_args(CSOUND + CSOUND_LIBRARY CSOUND_INCLUDE_DIR) +mark_as_advanced(CSOUND_INCLUDE_DIR CSOUND_LIBRARY) + +set(CSOUND_INCLUDE_DIRS ${CSOUND_INCLUDE_DIR}) +set(CSOUND_LIBRARIES ${CSOUND_LIBRARY} ) diff --git a/cmake/Modules/FindSDL2.cmake b/cmake/Modules/FindSDL2.cmake new file mode 100644 index 0000000..464ca2d --- /dev/null +++ b/cmake/Modules/FindSDL2.cmake @@ -0,0 +1,173 @@ + +# This module defines +# SDL2_LIBRARY, the name of the library to link against +# SDL2_FOUND, if false, do not try to link to SDL2 +# SDL2_INCLUDE_DIR, where to find SDL.h +# +# This module responds to the the flag: +# SDL2_BUILDING_LIBRARY +# If this is defined, then no SDL2main will be linked in because +# only applications need main(). +# Otherwise, it is assumed you are building an application and this +# module will attempt to locate and set the the proper link flags +# as part of the returned SDL2_LIBRARY variable. +# +# Don't forget to include SDLmain.h and SDLmain.m your project for the +# OS X framework based version. (Other versions link to -lSDL2main which +# this module will try to find on your behalf.) Also for OS X, this +# module will automatically add the -framework Cocoa on your behalf. +# +# +# Additional Note: If you see an empty SDL2_LIBRARY_TEMP in your configuration +# and no SDL2_LIBRARY, it means CMake did not find your SDL2 library +# (SDL2.dll, libsdl2.so, SDL2.framework, etc). +# Set SDL2_LIBRARY_TEMP to point to your SDL2 library, and configure again. +# Similarly, if you see an empty SDL2MAIN_LIBRARY, you should set this value +# as appropriate. These values are used to generate the final SDL2_LIBRARY +# variable, but when these values are unset, SDL2_LIBRARY does not get created. +# +# +# $SDL2DIR is an environment variable that would +# correspond to the ./configure --prefix=$SDL2DIR +# used in building SDL2. +# l.e.galup 9-20-02 +# +# Modified by Eric Wing. +# Added code to assist with automated building by using environmental variables +# and providing a more controlled/consistent search behavior. +# Added new modifications to recognize OS X frameworks and +# additional Unix paths (FreeBSD, etc). +# Also corrected the header search path to follow "proper" SDL guidelines. +# Added a search for SDL2main which is needed by some platforms. +# Added a search for threads which is needed by some platforms. +# Added needed compile switches for MinGW. +# +# On OSX, this will prefer the Framework version (if found) over others. +# People will have to manually change the cache values of +# SDL2_LIBRARY to override this selection or set the CMake environment +# CMAKE_INCLUDE_PATH to modify the search paths. +# +# Note that the header path has changed from SDL2/SDL.h to just SDL.h +# This needed to change because "proper" SDL convention +# is #include "SDL.h", not . This is done for portability +# reasons because not all systems place things in SDL2/ (see FreeBSD). + +#============================================================================= +# Copyright 2003-2009 Kitware, Inc. +# +# Distributed under the OSI-approved BSD License (the "License"); +# see accompanying file Copyright.txt for details. +# +# This software is distributed WITHOUT ANY WARRANTY; without even the +# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +# See the License for more information. +#============================================================================= +# (To distribute this file outside of CMake, substitute the full +# License text for the above reference.) + +# message("") + +SET(SDL2_SEARCH_PATHS + ~/Library/Frameworks + /Library/Frameworks + /usr/local + /usr + /sw # Fink + /opt/local # DarwinPorts + /opt/csw # Blastwave + /opt + ${SDL2_PATH} +) + +FIND_PATH(SDL2_INCLUDE_DIR SDL.h + HINTS + $ENV{SDL2DIR} + PATH_SUFFIXES include/SDL2 include + PATHS ${SDL2_SEARCH_PATHS} +) + +if(CMAKE_SIZEOF_VOID_P EQUAL 8) + set(PATH_SUFFIXES lib64 lib/x64 lib) +else() + set(PATH_SUFFIXES lib/x86 lib) +endif() + +FIND_LIBRARY(SDL2_LIBRARY_TEMP + NAMES SDL2 + HINTS + $ENV{SDL2DIR} + PATH_SUFFIXES ${PATH_SUFFIXES} + PATHS ${SDL2_SEARCH_PATHS} +) + +IF(NOT SDL2_BUILDING_LIBRARY) + IF(NOT ${SDL2_INCLUDE_DIR} MATCHES ".framework") + # Non-OS X framework versions expect you to also dynamically link to + # SDL2main. This is mainly for Windows and OS X. Other (Unix) platforms + # seem to provide SDL2main for compatibility even though they don't + # necessarily need it. + FIND_LIBRARY(SDL2MAIN_LIBRARY + NAMES SDL2main + HINTS + $ENV{SDL2DIR} + PATH_SUFFIXES ${PATH_SUFFIXES} + PATHS ${SDL2_SEARCH_PATHS} + ) + ENDIF(NOT ${SDL2_INCLUDE_DIR} MATCHES ".framework") +ENDIF(NOT SDL2_BUILDING_LIBRARY) + +# SDL2 may require threads on your system. +# The Apple build may not need an explicit flag because one of the +# frameworks may already provide it. +# But for non-OSX systems, I will use the CMake Threads package. +IF(NOT APPLE) + FIND_PACKAGE(Threads) +ENDIF(NOT APPLE) + +# MinGW needs an additional link flag, -mwindows +# It's total link flags should look like -lmingw32 -lSDL2main -lSDL2 -mwindows +IF(MINGW) + SET(MINGW32_LIBRARY mingw32 "-mwindows" CACHE STRING "mwindows for MinGW") +ENDIF(MINGW) + +IF(SDL2_LIBRARY_TEMP) + # For SDL2main + IF(NOT SDL2_BUILDING_LIBRARY) + IF(SDL2MAIN_LIBRARY) + SET(SDL2_LIBRARY_TEMP ${SDL2MAIN_LIBRARY} ${SDL2_LIBRARY_TEMP}) + ENDIF(SDL2MAIN_LIBRARY) + ENDIF(NOT SDL2_BUILDING_LIBRARY) + + # For OS X, SDL2 uses Cocoa as a backend so it must link to Cocoa. + # CMake doesn't display the -framework Cocoa string in the UI even + # though it actually is there if I modify a pre-used variable. + # I think it has something to do with the CACHE STRING. + # So I use a temporary variable until the end so I can set the + # "real" variable in one-shot. + IF(APPLE) + SET(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} "-framework Cocoa") + ENDIF(APPLE) + + # For threads, as mentioned Apple doesn't need this. + # In fact, there seems to be a problem if I used the Threads package + # and try using this line, so I'm just skipping it entirely for OS X. + IF(NOT APPLE) + SET(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} ${CMAKE_THREAD_LIBS_INIT}) + ENDIF(NOT APPLE) + + # For MinGW library + IF(MINGW) + SET(SDL2_LIBRARY_TEMP ${MINGW32_LIBRARY} ${SDL2_LIBRARY_TEMP}) + ENDIF(MINGW) + + # Set the final string here so the GUI reflects the final state. + SET(SDL2_LIBRARY ${SDL2_LIBRARY_TEMP} CACHE STRING "Where the SDL2 Library can be found") + # Set the temp variable to INTERNAL so it is not seen in the CMake GUI + SET(SDL2_LIBRARY_TEMP "${SDL2_LIBRARY_TEMP}" CACHE INTERNAL "") +ENDIF(SDL2_LIBRARY_TEMP) + +# message("") + +INCLUDE(FindPackageHandleStandardArgs) + +FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2 REQUIRED_VARS SDL2_LIBRARY SDL2_INCLUDE_DIR) diff --git a/examples/test.csd b/examples/test.csd new file mode 100644 index 0000000..3e528cf --- /dev/null +++ b/examples/test.csd @@ -0,0 +1,57 @@ + + +-odac +--opcode-lib=/home/bargepole/bpcheckout/csound-dev/sdltest/sdllib.so + + +sr = 44100 +kr = 4410 +nchnls = 2 +0dbfs = 1 +seed 0 + +gisdl init -1 +gicol init -1 + +instr 1 + gisdl, ktrig, kx, ky sdlinit "A SDL Window", 1000, 1000, 60 + kvibe oscil 2, 0.1 + kvibe2 oscil 0.2, 0.5 + kcol oscil 250, 0.3 + gicol sdlcolour abs(kcol), 50, 50, 0 + + schedkwhen ktrig, 0.05, 0, 2, 0, 1, kx, ky + + + kl1 line 1, p3, 0 + sdlrect gisdl, gicol, kl1, 0.5, 0.2, abs(kvibe)*0.2 + ao oscil 1, 440*abs(kvibe) + outs ao*0.01, ao*0.01 +endin + +instr 2 + krest line 0, p3, 255 + icol sdlcolour 255, krest, krest, 0 + kx init p4 + ky init p5 + sdlrect gisdl, icol, kx, ky, 0.1, 0.1 + + iamp = 0.3 + (p5*0.7) + kamp line iamp, p3, 0 + ipitch = 100 + (p4*400) + a1 oscil kamp, ipitch + outs a1*0.01, a1*0.01 +endin + +instr 3 + icol sdlcolour 0, 0, 0, 0 + kx line 0, p3, 1 + sdlline gisdl, icol, kx, 0, kx, 1 +endin + + + +i1 0 60 +i3 0 10 + + diff --git a/src/opcodes.cpp b/src/opcodes.cpp new file mode 100644 index 0000000..1531029 --- /dev/null +++ b/src/opcodes.cpp @@ -0,0 +1,278 @@ +#include +#include + + +struct SDLColour { + int r; + int g; + int b; + int a; +}; + +struct SDLSession { + SDL_Window* window = NULL; + SDL_Renderer* renderer = NULL; + int screen_width; + int screen_height; + bool initialised = false; + bool cycle = false; +}; + +const char* sessionName = "::sdlsession%d"; +const char* colourName = "::sdlcolour%d"; +const char* badHandle = "cannot obtain data from handle"; + +/* + * Create connection in global variables returning handle + */ + +MYFLT createHandle(csnd::Csound* csound, SDLSession** sdl) { + char buffer[32]; + int handle = 0; + snprintf(buffer, 32, sessionName, handle); + while ((*sdl = (SDLSession*) csound->query_global_variable(buffer)) != NULL) { + snprintf(buffer, 32, sessionName, ++handle); + } + csound->create_global_variable(buffer, sizeof(SDLSession)); + *sdl = (SDLSession*) csound->query_global_variable(buffer); + + return FL(handle); +} + +MYFLT CreateColour(csnd::Csound* csound, SDLColour** sdlc) { + char buffer[32]; + int handle = 0; + snprintf(buffer, 32, colourName, handle); + while ((*sdlc = (SDLColour*) csound->query_global_variable(buffer)) != NULL) { + snprintf(buffer, 32, colourName, ++handle); + } + csound->create_global_variable(buffer, sizeof(SDLColour)); + *sdlc = (SDLColour*) csound->query_global_variable(buffer); + + return FL(handle); +} + +SDLSession* getSession(csnd::Csound* csound, MYFLT handle) { + char buffer[32]; + snprintf(buffer, 32, sessionName, (int)handle); + return (SDLSession*) csound->query_global_variable(buffer); +} + +SDLColour* getColour(csnd::Csound* csound, MYFLT handle) { + char buffer[32]; + snprintf(buffer, 32, colourName, (int)handle); + return (SDLColour*) csound->query_global_variable(buffer); +} + +// ihandle, kmouseTrigger, kmouseXratio, kmouseYratio sdlinit SwindowName, iWidth, iHeight, ifps +struct sdlinit : csnd::Plugin<4, 4> { + static constexpr char const *otypes = "ikkk"; + static constexpr char const *itypes = "Siii"; + SDLSession* sdl; + uint64_t kcnt; + uint64_t frameK; + bool mouseDown; + + int init() { + csound->plugin_deinit(this); + outargs[0] = createHandle(csound, &sdl); + mouseDown = false; + + STRINGDAT &windowName = inargs.str_data(0); + int xSize = (int) inargs[1]; + int ySize = (int) inargs[2]; + int fps = (int) inargs[3]; + + try { + init_sdl(windowName.data, xSize, ySize); + } catch (const std::exception &e) { + return csound->init_error(e.what()); + } + + kcnt = insdshead->kcounter; + frameK = (uint64_t) (FL(FL(1)/FL(fps)) / insdshead->onedkr); + + return OK; + } + + void init_sdl(char* windowName, int xSize, int ySize) { + if (sdl->initialised) { + return; + } + sdl->screen_width = xSize; + sdl->screen_height = ySize; + + if (SDL_Init(SDL_INIT_VIDEO) < 0) { + throw std::runtime_error(csound->strdup(SDL_GetError())); + } + SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"); + sdl->window = SDL_CreateWindow(windowName, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, xSize, ySize, SDL_WINDOW_SHOWN ); + if (sdl->window == NULL) { + throw std::runtime_error(csound->strdup(SDL_GetError())); + } + + sdl->renderer = SDL_CreateRenderer(sdl->window, -1, SDL_RENDERER_ACCELERATED); + if (sdl->renderer == NULL) { + throw std::runtime_error(csound->strdup(SDL_GetError())); + } + sdl->initialised = true; + } + + int deinit() { + SDL_DestroyRenderer(sdl->renderer); + SDL_DestroyWindow(sdl->window); + sdl->window = NULL; + sdl->renderer = NULL; + sdl->initialised = false; + SDL_Quit(); + return OK; + } + + void setMouse() { + int x; + int y; + SDL_GetMouseState(&x, &y); + outargs[1] = 1; + outargs[2] = (MYFLT) (FL(x) / FL(sdl->screen_width)); + outargs[3] = (MYFLT) (FL(y) / FL(sdl->screen_height)); + } + + int kperf() { + if (insdshead->kcounter % frameK != 0) { + sdl->cycle = false; + return OK; + } + SDL_Event e; + outargs[1] = 0; + outargs[2] = 0; + outargs[3] = 0; + while (SDL_PollEvent(&e) != 0) { + // e.type == SDL_MOUSEMOTION || + // || e.type == SDL_MOUSEBUTTONUP + if (e.type == SDL_MOUSEBUTTONDOWN || (mouseDown && e.type == SDL_MOUSEMOTION)) { + setMouse(); + mouseDown = true; + } + if (e.type == SDL_MOUSEBUTTONUP) { + mouseDown = false; + } + } + + sdl->cycle = true; + SDL_RenderPresent(sdl->renderer); + SDL_SetRenderDrawColor(sdl->renderer, 0xFF, 0xFF, 0xFF, 0xFF ); + SDL_RenderClear(sdl->renderer); + return OK; + } +}; + +// ihandle sdlcolour red, green, blue, alpha +struct sdlcolour : csnd::Plugin<1, 4> { + static constexpr char const *otypes = "i"; + static constexpr char const *itypes = "kkkk"; + SDLColour* sdlc; + + int init() { + outargs[0] = CreateColour(csound, &sdlc); + return OK; + } + + int kperf() { + sdlc->r = (int) inargs[0]; + sdlc->g = (int) inargs[1]; + sdlc->b = (int) inargs[2]; + sdlc->a = (int) inargs[3]; + return OK; + } +}; + + +// sdlrect ihandle, icolourhandle, kx, ky, kwidth, kheight +struct sdlrect : csnd::InPlug<6> { + static constexpr char const *otypes = ""; + static constexpr char const *itypes = "iikkkk"; + SDLSession* sdl; + SDLColour* sdlc; + + + int init() { + if (!(sdl = getSession(csound, args[0]))) { + return csound->init_error(badHandle); + } + + if (!(sdlc = getColour(csound, args[1]))) { + return csound->init_error(badHandle); + } + + return OK; + } + + int kperf() { + if (!sdl->cycle) { + return OK; + } + + int xpos = (int) sdl->screen_width * args[2]; + int ypos = (int) sdl->screen_height * args[3]; + int width = (int) sdl->screen_width * args[4]; + int height = (int) sdl->screen_height * args[5]; + + SDL_Rect fillRect = { + xpos, ypos, width, height + }; + + SDL_SetRenderDrawColor(sdl->renderer, sdlc->r, sdlc->g, sdlc->b, sdlc->a); + SDL_RenderFillRect(sdl->renderer, &fillRect); + + return OK; + } +}; + + +struct sdlline : csnd::InPlug<6> { + static constexpr char const *otypes = ""; + static constexpr char const *itypes = "iikkkk"; + SDLSession* sdl; + SDLColour* sdlc; + + + int init() { + if (!(sdl = getSession(csound, args[0]))) { + return csound->init_error(badHandle); + } + + if (!(sdlc = getColour(csound, args[1]))) { + return csound->init_error(badHandle); + } + + return OK; + } + + int kperf() { + if (!sdl->cycle) { + return OK; + } + + int xpos1 = (int) sdl->screen_width * args[2]; + int ypos1 = (int) sdl->screen_height * args[3]; + int xpos2 = (int) sdl->screen_width * args[4]; + int ypos2 = (int) sdl->screen_height * args[5]; + + SDL_SetRenderDrawColor(sdl->renderer, sdlc->r, sdlc->g, sdlc->b, sdlc->a); + SDL_RenderDrawLine(sdl->renderer, xpos1, ypos1, xpos2, ypos2); + + return OK; + } +}; + + + +#include + +void csnd::on_load(csnd::Csound *csound) { + + csnd::plugin(csound, "sdlinit", csnd::thread::ik); + csnd::plugin(csound, "sdlrect", csnd::thread::ik); + csnd::plugin(csound, "sdlcolour", csnd::thread::ik); + csnd::plugin(csound, "sdlline", csnd::thread::ik); +} diff --git a/src/tests.cpp b/src/tests.cpp new file mode 100644 index 0000000..2ca0ea4 --- /dev/null +++ b/src/tests.cpp @@ -0,0 +1,171 @@ +/*This source code copyrighted by Lazy Foo' Productions (2004-2019) +and may not be redistributed without written permission.*/ + +//Using SDL, SDL_image, standard IO, math, and strings +#include + +#include +#include +#include + +//Screen dimension constants +const int SCREEN_WIDTH = 640; +const int SCREEN_HEIGHT = 480; + +//Starts up SDL and creates window +bool init(); + +//Loads media +bool loadMedia(); + +//Frees media and shuts down SDL +void close(); + +//Loads individual image as texture +SDL_Texture* loadTexture( std::string path ); + +//The window we'll be rendering to +SDL_Window* gWindow = NULL; + +//The window renderer +SDL_Renderer* gRenderer = NULL; + +bool init() +{ + //Initialization flag + bool success = true; + + //Initialize SDL + if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) + { + printf( "SDL could not initialize! SDL Error: %s\n", SDL_GetError() ); + success = false; + } + else + { + //Set texture filtering to linear + if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) ) + { + printf( "Warning: Linear texture filtering not enabled!" ); + } + + //Create window + gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN ); + if( gWindow == NULL ) + { + printf( "Window could not be created! SDL Error: %s\n", SDL_GetError() ); + success = false; + } + else + { + //Create renderer for window + gRenderer = SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED ); + if( gRenderer == NULL ) + { + printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() ); + success = false; + } + else + { + //Initialize renderer color + SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF ); + + } + } + } + + return success; +} + +bool loadMedia() +{ + //Loading success flag + bool success = true; + + //Nothing to load + return success; +} + +void close() +{ + //Destroy window + SDL_DestroyRenderer( gRenderer ); + SDL_DestroyWindow( gWindow ); + gWindow = NULL; + gRenderer = NULL; + + //Quit SDL subsystems + SDL_Quit(); +} + +int main( int argc, char* args[] ) +{ + //Start up SDL and create window + if( !init() ) + { + printf( "Failed to initialize!\n" ); + } + else + { + //Load media + if( !loadMedia() ) + { + printf( "Failed to load media!\n" ); + } + else + { + //Main loop flag + bool quit = false; + + //Event handler + SDL_Event e; + + //While application is running + while( !quit ) + { + //Handle events on queue + while( SDL_PollEvent( &e ) != 0 ) + { + //User requests quit + if( e.type == SDL_QUIT ) + { + quit = true; + } + } + + //Clear screen + SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF ); + SDL_RenderClear( gRenderer ); + + //Render red filled quad + SDL_Rect fillRect = { SCREEN_WIDTH / 4, SCREEN_HEIGHT / 4, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 }; + SDL_SetRenderDrawColor( gRenderer, 0xFF, 0x00, 0x00, 0xFF ); + SDL_RenderFillRect( gRenderer, &fillRect ); + + //Render green outlined quad + SDL_Rect outlineRect = { SCREEN_WIDTH / 6, SCREEN_HEIGHT / 6, SCREEN_WIDTH * 2 / 3, SCREEN_HEIGHT * 2 / 3 }; + SDL_SetRenderDrawColor( gRenderer, 0x00, 0xFF, 0x00, 0xFF ); + SDL_RenderDrawRect( gRenderer, &outlineRect ); + + //Draw blue horizontal line + SDL_SetRenderDrawColor( gRenderer, 0x00, 0x00, 0xFF, 0xFF ); + SDL_RenderDrawLine( gRenderer, 0, SCREEN_HEIGHT / 2, SCREEN_WIDTH, SCREEN_HEIGHT / 2 ); + + //Draw vertical line of yellow dots + SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0x00, 0xFF ); + for( int i = 0; i < SCREEN_HEIGHT; i += 4 ) + { + SDL_RenderDrawPoint( gRenderer, SCREEN_WIDTH / 2, i ); + } + + //Update screen + SDL_RenderPresent( gRenderer ); + } + } + } + + //Free resources and close SDL + close(); + + return 0; +} \ No newline at end of file -- cgit v1.2.3